linaria icon indicating copy to clipboard operation
linaria copied to clipboard

Linaria + jest + absolute path imports = crash

Open nstepien opened this issue 4 years ago β€’ 3 comments

Environment

  • Linaria version: 3.0.0-beta.7
  • Bundler (+ version): [email protected]
  • Node.js version: v16.4.0
  • OS: win10

Description

We have some absolute path imports to modules under src, which works fine with webpack builds as we use aliases, but Linaria doesn't handle them as well with Jest's moduleDirectories. For now we avoid this issue by using relative paths when Linaria needs it.

Reproducible Demo

ReproStyles/index.ts:

import { css } from '@linaria/core';

export const reproClassName = css``;

demo.test.ts:

import { css } from '@linaria/core';
import { reproClassName } from 'ReproStyles';

const testClass = css`
  .${reproClassName} {
    color: red;
  }
`;

console.log(testClass);

test('works', () => {
  expect(1).toBe(1);
});

jest.config.mjs:

export default {
  rootDir: 'src',
  testMatch: ['**/*.test.(ts|tsx)'],
  testEnvironment: 'jsdom',
  moduleDirectories: ['src', 'node_modules']
};

Error:

> jest "demo.test"

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

    D:\xxx\src\demo.test.ts: An unexpected runtime error occurred during dependencies evaluation:
    node:internal/modules/cjs/loader:930
      throw err;
      ^

    Error: Cannot find module 'ReproStyles'
    Require stack:
    - D:\xxx\src\demo.test.ts
        at Function._resolveFilename (node:internal/modules/cjs/loader:927:15)
        at Function._resolveFilename (D:\xxx\node_modules\@linaria\babel-preset\src\module.ts:364:5)
        at Module.resolve (D:\xxx\node_modules\@linaria\babel-preset\src\module.ts:172:21)
        at Module.Object.assign.ensure (D:\xxx\node_modules\@linaria\babel-preset\src\module.ts:200:29)
        at D:\xxx\src\demo.test.ts:3:20
        at D:\xxx\src\demo.test.ts:16:3
        at Script.runInContext (node:vm:139:12)
        at Module.evaluate (D:\xxx\node_modules\@linaria\babel-preset\src\module.ts:326:12)
        at evaluate (D:\xxx\node_modules\@linaria\babel-preset\src\evaluators\index.ts:16:5)
        at PluginPass.enter (D:\xxx\node_modules\@linaria\babel-preset\src\extract.ts:177:34)

    It may happen when your code or third party module is invalid or uses identifiers not available in Node environment, eg. window.
    Note that line numbers in above stack trace will most likely not match, because Linaria needed to transform your code a bit.


      1 | import { css } from '@linaria/core';
      2 | import { reproClassName } from 'ReproStyles';
    > 3 |
        | ^
      4 | const testClass = css`
      5 |   .${reproClassName} {
      6 |     color: red;

      at Function._resolveFilename (../node_modules/@linaria/babel-preset/src/module.ts:364:5)
      at Module.resolve (../node_modules/@linaria/babel-preset/src/module.ts:172:21)
      at Module.Object.assign.ensure (../node_modules/@linaria/babel-preset/src/module.ts:200:29)
      at demo.test.ts:3:20
      at demo.test.ts:16:3
      at Module.evaluate (../node_modules/@linaria/babel-preset/src/module.ts:326:12)
      at evaluate (../node_modules/@linaria/babel-preset/src/evaluators/index.ts:16:5)
      at PluginPass.enter (../node_modules/@linaria/babel-preset/src/extract.ts:177:34)
      It may happen when your code or third party module is invalid or uses identifiers not available in Node environment, eg. window.
      Note that line numbers in above stack trace will most likely not match, because Linaria needed to transform your code a bit.
      at PluginPass.enter (../node_modules/@linaria/babel-preset/src/extract.ts:189:21)
      at newFn (../node_modules/@babel/traverse/lib/visitors.js:171:21)
      at NodePath._call (../node_modules/@babel/traverse/lib/path/context.js:53:20)
      at NodePath.call (../node_modules/@babel/traverse/lib/path/context.js:40:17)
      at NodePath.visit (../node_modules/@babel/traverse/lib/path/context.js:90:31)
      at TraversalContext.visitQueue (../node_modules/@babel/traverse/lib/context.js:99:16)
      at TraversalContext.visitSingle (../node_modules/@babel/traverse/lib/context.js:73:19)
      at TraversalContext.visit (../node_modules/@babel/traverse/lib/context.js:127:19)
      at Function.traverse.node (../node_modules/@babel/traverse/lib/index.js:76:17)
      at traverse (../node_modules/@babel/traverse/lib/index.js:56:12)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        5.379 s
Ran all test suites matching /demo.test/i.

nstepien avatar Jun 30 '21 09:06 nstepien

Same issue, any update guys? Thanks

Spaider15 avatar Oct 22 '21 10:10 Spaider15

Module._resolve invokes node’s Module._resolveFilename with whatever path it sees. That can't work with path aliases (or anything else _resolveFilename won't understand).

How can this be made to work?

Module._resolveFilename is not aware of Jest's moduleNameMapper.

dgcoffman avatar Dec 22 '21 21:12 dgcoffman

Seems to be another instance of https://github.com/callstack/linaria/issues/531

dgcoffman avatar Dec 22 '21 22:12 dgcoffman

@Anber This is still happening with @linaria/[email protected] and [email protected], this is affecting our biggest repo.

> jest src/demo

 FAIL  src/demo.test.ts
  ● Test suite failed to run
                                                                                                                       
    EvalError: D:\repo\src\demo.test.ts: Cannot find module 'ReproStyles'     
    Require stack:
    - D:\repo\src\demo.test.ts in
    | D:\repo\src\demo.test.ts

      at node_modules/@linaria/babel-preset/src/module.ts:475:15
          at Array.forEach (<anonymous>)
      at Module.forEach [as evaluate] (node_modules/@linaria/babel-preset/src/module.ts:437:10)
      at evaluate (node_modules/@linaria/babel-preset/src/evaluators/index.ts:28:5)
      at evalStage (node_modules/@linaria/babel-preset/src/transform-stages/2-eval.ts:32:29)
      at PluginPass.pre (node_modules/@linaria/babel-preset/src/plugins/babel-transform.ts:54:40)
      at call (node_modules/@babel/core/src/transformation/index.ts:97:27)
          at transformFile.next (<anonymous>)
      at transformFile (node_modules/@babel/core/src/transformation/index.ts:47:12)
          at run.next (<anonymous>)
      at transform (node_modules/@babel/core/src/transform.ts:29:20)
          at transform.next (<anonymous>)
      at evaluateSync (node_modules/gensync/index.js:251:28)
      at sync (node_modules/gensync/index.js:89:14)
      at fn (node_modules/@babel/core/src/errors/rewrite-stack-trace.ts:97:14)
      at transformSync (node_modules/@babel/core/src/transform.ts:66:52)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:519:31)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:648:40)   
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:700:19)

Somehow our tests work fine with linaria 3.0.0-beta.22 though. I think some absolute paths get correctly resolved, but not all with this older version.

nstepien avatar Dec 09 '22 15:12 nstepien

Linaria doesn't need to be transpiled anymore for tests, so I'm closing this.

nstepien avatar May 04 '23 11:05 nstepien