Code Monkey home page Code Monkey logo

node-tree-sitter-bug-minimal-reproducible-example's Introduction

The bug

yarn run v1.19.1
PASS src/lib/__test__/two.test.js
FAIL src/lib/__test__/one.test.js
  โ— Test suite failed to run

    TypeError: Illegal invocation

    > 1 | const Parser = require("tree-sitter");
        | ^
      2 | 
      3 | class Tokenizer {
      4 | 

      at Object.get [as rootNode] (node_modules/tree-sitter/index.js:20:35)
      at Object.<anonymous> (node_modules/tree-sitter/index.js:16:26)
      at Object.<anonymous> (src/lib/tokenizer.js:1:1)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.889s, estimated 1s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

How to reproduce?

# Clone example
git clone [email protected]:rien/node-tree-sitter-bug-minimal-reproducible-example.git
cd node-tree-sitter-bug-minimal-reproducible-example

# Install dependencies
yarn install

# Run jest with all tests
yarn run jest # will possibly succeed
yarn run jest # will fail with 'TypeError: invalid invocation'

# Run jest a single test at a time
yarn run test src/lib/__test__/one.test.ts # succeeds
yarn run test src/lib/__test__/two.test.ts # succeeds

# Run all 100+ ava tests
yarn run ava # succeeds

Running yarn run jest --clearCache sometimes solves this issue for a few runs.

Cause

Jest overrides the require behaviour, which causes the native code to be initialized more than once. Only the first initialization (wrapping a native object) succeeds, causing the initializations thereafter to fail.

Fix

A possible fix is to bind the module that can only be included once to a global variable, like in this commit:

https://github.com/rien/node-tree-sitter-bug-minimal-reproducible-example/commit/57606393c8977d72f926e866abf3996ac75c87a4

However, if your project is using TypeScript this will become very messy to let this work properly.

node-tree-sitter-bug-minimal-reproducible-example's People

Contributors

rien avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.