Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test suite doesnt work with dependencies #11

Open
seanaye opened this issue Oct 5, 2021 · 1 comment
Open

Test suite doesnt work with dependencies #11

seanaye opened this issue Oct 5, 2021 · 1 comment

Comments

@seanaye
Copy link

seanaye commented Oct 5, 2021

Expected Behavior

Simple jest tests should work when there are project dependencies that use ESM syntax.

Actual Behavior

If any added dependency has ESM exports, jest tests will fail and complain about unexpected tokens

FAIL src/utils.test.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /Users/seanaye/dev/coparse/filesystem/node_modules/idb/with-async-ittr.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './build/esm/index.js';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token 'export'

      1 | import { FileHandle, FileSource, FileSystemFile, LegacyFile } from "./file";
    > 2 | import { DBSchema, IDBPDatabase, openDB } from "idb/with-async-ittr.js";

I have tried all of the things suggested by the error message but nothing works.

Steps to Reproduce the Problem

  1. Add a project dependency with ESM exports in my case this is idb
  2. Try to run a jest test in which this dependency is imported
  3. Test failure

Specifications

  • Version: 0.4.2
  • Platform: MacOS, Node 16.1
@laustdeleuran
Copy link

laustdeleuran commented Nov 9, 2021

+1

SyntaxError: Cannot use import statement outside a module

Steps to reproduce

  1. Clone this repo.
  2. Add dependencies: yarn add rehype-stringify remark-gfm remark-parse remark-rehype unified
  3. Add utility function and corresponding test:
    // src/format.ts
    import rehypeStringify from "rehype-stringify";
    import remarkGfm from "remark-gfm";
    import remarkParse from "remark-parse";
    import remarkRehype from "remark-rehype";
    import { unified } from "unified";
    
    export const format = (markdown: string) =>
      unified()
        .use(remarkParse)
        .use(remarkGfm)
        .use(remarkRehype)
        .use(rehypeStringify)
        .processSync(markdown)
        .toString();
    // src/format.test.ts
    import { format } from "./format";
    
    describe("format", () => {
      it("should parse markdown", () => {
        expect(format("# Headline")).toBe("<h1>Headline</h1>");
      });
    });
  4. Run yarn test and see error output:
     FAIL  src/format.test.ts
      ● Test suite failed to run
    
        Jest encountered an unexpected token
    
        This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
    
        By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
    
        Here's what you can do:
         • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
         • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
         • If you need a custom transformation specify a "transform" option in your config.
         • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
    
        You'll find more details and examples of these config options in the docs:
        https://jestjs.io/docs/en/configuration.html
    
        Details:
    
        /typescript-boilerplate/node_modules/remark-gfm/index.js:6
        import {gfm} from 'micromark-extension-gfm'
        ^^^^^^
    
        SyntaxError: Cannot use import statement outside a module
    
        > 1 | import remarkGfm from "remark-gfm";
            | ^
          2 | import remarkParse from "remark-parse";
          3 | import remarkRehype from "remark-rehype";
          4 | import { unified } from "unified";
    
          at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
          at Object.<anonymous> (src/format.ts:1:1)
    
    Test Suites: 1 failed, 1 passed, 2 total
    
  5. Weep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants