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

Tests are failing when running with plugin but working fine with command line #408

Closed
unfalse opened this issue Nov 28, 2018 · 14 comments · Fixed by #674
Closed

Tests are failing when running with plugin but working fine with command line #408

unfalse opened this issue Nov 28, 2018 · 14 comments · Fixed by #674

Comments

@unfalse
Copy link

unfalse commented Nov 28, 2018

Environment

  1. node -v: v10.13.0

  2. npm -v: 6.4.1

  3. npm ls jest or npm ls react-scripts (if you haven’t ejected):
    $ npm ls jest
    jest_vscode_failing_example_app@1.0.0 C:\MyExperiments\jest_vscode_failing_example_app
    `-- jest@22.4.4
    (also tried 23.6.0 with the same results)

  4. your vscode-jest settings if customized:

    • jest.pathToJest? null
    • jest.pathToConfig? ""
    • anything else that you think might be relevant? no
  5. Operating system: Windows 10 Version 1803 (OS Build 17134.407)

Prerequisite

  • are you able to run jest test from command line? yes
  • how do you run your tests from command line? (for example: npm run test or node_modules/.bin/jest) npm run test

Steps to Reproduce

https://github.com/unfalse/vscode_jest_fail

Relevant Debug Info

This is the messages from the output window.

--watch is not supported without git/hg, please use --watchAll 

  console.warn node_modules/react-addons-test-utils/index.js:31
    Warning: ReactTestUtils has been moved to react-dom/test-utils. Update references to remove this warning.


FAIL src/index.test.js

  ● 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:
     • 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:

    SyntaxError: C:\MyExperiments\jest_vscode_failing_example_app\src\index.test.js: Unexpected token (7:30)

       5 | describe('App: ', () => {
       6 |     it('renders withour crash', () => {
    >  7 |         var wrapper = shallow(<App/>);
         |                               ^
       8 |         expect(wrapper.find('div').length).toEqual(1);
       9 |     });
      10 | });

      at Parser.raise (node_modules/@babel/parser/lib/index.js:4028:15)
      at Parser.unexpected (node_modules/@babel/parser/lib/index.js:5343:16)
      at Parser.parseExprAtom (node_modules/@babel/parser/lib/index.js:6432:20)
      at Parser.parseExprSubscripts (node_modules/@babel/parser/lib/index.js:6019:21)
      at Parser.parseMaybeUnary (node_modules/@babel/parser/lib/index.js:5998:21)
      at Parser.parseExprOps (node_modules/@babel/parser/lib/index.js:5907:21)
      at Parser.parseMaybeConditional (node_modules/@babel/parser/lib/index.js:5879:21)
      at Parser.parseMaybeAssign (node_modules/@babel/parser/lib/index.js:5826:21)
      at Parser.parseExprListItem (node_modules/@babel/parser/lib/index.js:7111:18)
      at Parser.parseCallExpressionArguments (node_modules/@babel/parser/lib/index.js:6227:22)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        4.518s
Ran all test suites.

Expected Behavior

vscode-jest is not failing on tests.

Actual Behavior

vscode-jest failing on tests but npm run test works fine.


@connectdotz
Copy link
Collaborator

from toubleshooting :

you can easily use the jest.pathToJest or jest.pathToConfig settings to instruct the plugin on how to start jest. You can even use the scripts from package.json, such as npm run test -- or yarn test.

@unfalse
Copy link
Author

unfalse commented Nov 29, 2018

from toubleshooting :

you can easily use the jest.pathToJest or jest.pathToConfig settings to instruct the plugin on how to start jest. You can even use the scripts from package.json, such as npm run test -- or yarn test.

Tried

jest.pathToConfig: src/test/testSetup.js
jest.pathToJest: node_modules/.bin/jest

output:

Warning: ReactTestUtils has been moved to react-dom/test-utils. Update references to remove this warning.

No tests found
In c:\MyExperiments\jest_vscode_failing_example_app\src\test
  3 files checked.
  testMatch: **/__tests__/**/*.js?(x),**/?(*.)+(spec|test).js?(x) - 0 matches
  testPathIgnorePatterns: \\node_modules\\ - 3 matches
Pattern:  - 0 matches

Finished running all tests. Starting watch mode.

@connectdotz
Copy link
Collaborator

how do you run your tests from command line? (for example: npm run test or node_modules/.bin/jest) npm run test

did you try jest.pathToConfig: npm run test --?

@unfalse
Copy link
Author

unfalse commented Nov 29, 2018

how do you run your tests from command line? (for example: npm run test or node_modules/.bin/jest) npm run test

did you try jest.pathToConfig: npm run test --?

you mean "jest.pathToJest" ?
Yes it helped but i'm not sure if it really works. I'll check it later.

@unfalse
Copy link
Author

unfalse commented Nov 29, 2018

Jest shows that tests are ok but I changed intentionally one test and reloaded vscode window. Nothing has changed.
Also it doesn't work without jest.pathToConfig: src/test/testSetup.js

@connectdotz
Copy link
Collaborator

ok, you are making progress...

Also it doesn't work without jest.pathToConfig: src/test/testSetup.js

Basically you need to tell the plugin about your env, which means to setup jest.pathToJest and jest.pathToConfig if needed.

Jest shows that tests are ok but I changed intentionally one test and reloaded vscode window. Nothing has changed.

meaning every tests shows empty circle? What does the output console show? Also in your post, I saw

--watch is not supported without git/hg, please use --watchAll

is your repo under git? The plugin does run watch mode, if your repo can't be "watched", it won't show status...

@unfalse
Copy link
Author

unfalse commented Nov 29, 2018

meaning every tests shows empty circle?

Yes an empty green circle.

is your repo under git?

Yes it is.

Output:

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Username\AppData\Roaming\npm-cache\_logs\2018-11-29T14_50_52_307Z-debug.log

> jest_vscode_failing_example_app@1.0.0 test-fast c:\MyExperiments\jest_vscode_failing_example_app
> jest "--json" "--useStderr" "--outputFile" "C:\Users\Username\AppData\Local\Temp/jest_runner.json" "--watchAll" "--no-color" "--config" "src\test\testSetup.js"


Warning: ReactTestUtils has been moved to react-dom/test-utils. Update references to remove this warning.

No tests found
In c:\MyExperiments\jest_vscode_failing_example_app\src\test
  3 files checked.
  testMatch: **/__tests__/**/*.js?(x),**/?(*.)+(spec|test).js?(x) - 0 matches
  testPathIgnorePatterns: \\node_modules\\ - 3 matches
Pattern:  - 0 matches

@connectdotz
Copy link
Collaborator

I downloaded your repo to my mac, vscode-jest worked flawlessly without any customization as far as I can see. Change the test file also reflected as expected.

the error:

No tests found
In c:\MyExperiments\jest_vscode_failing_example_app\src\test

I looked at your repo, there is indeed no test in that directory...

not sure if this is a windows specific issue, in that case maybe somebody else with a windows env is better suited to help...

@unfalse
Copy link
Author

unfalse commented Dec 4, 2018

I have even moved testSetup.js to src and now I got just the same output as in my first message.

@SgtPooki
Copy link

SgtPooki commented Aug 2, 2019

@connectdotz I am also getting this issue on macosx high sierra .. I'm running a monorepo though. When I open the workspace at my monorepo root, the test is failing. When I open the individual package root directly in vscode, it passes.

@matteoATDY
Copy link

matteoATDY commented Sep 24, 2020

I have some tests running fine in terminal (both on windows and linux/ubuntu) and vs-code on windows, but failing in vs-code on linux/ubuntu.

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error

@unfalse
Copy link
Author

unfalse commented Dec 2, 2020

I simply gave up on vscode-jest. Our tests are very sluggish and this plugin just stresses it out. I'm happy with jest command line ability to launch a separate test instead of all tests at once and the attach ability of vscode debugger. Maybe if I could setup this plugin to run with --runInBand and --detectOpenHandles jest arguments it would be more usable. But my pc is lagging because of tests running by plugin in background.

@connectdotz
Copy link
Collaborator

For people with performance issues or having problems getting the extension to start for your environment, please give v4.0.0-alpha.5 a try, it has now an interactive mode that users can control what test to run and when; as well as a "Setup Wizard" to help set up the extension for you.

@IntusFacultas
Copy link

Re-opening this issue, I'm experiencing this both on v5 and v6

Environment

node -v: v16.16.0

npm -v: 8.11.0

npm ls jest

├─┬ jest-extended@4.0.0
│ └── jest@29.5.0 deduped
├── jest@29.5.0
└─┬ ts-jest@29.1.0
  └── jest@29.5.0 deduped

Operating system: macOS Ventura v13.4.1

Prerequisite

  1. Are you able to run jest test from command line? Yes.
  2. How do you run your tests from command line? npm run test (npx jest --coverage)

Relevant Debug Information

TSConfig.json

{
  "compilerOptions": {
    "target": "ES2019",
    "module": "commonjs",
    "lib": [
      "DOM",
      "DOM.Iterable",
      "ES2019"
    ],
    "paths": {
      "src": ["./src",],
      "src/*": ["./src/*",],
    },
    "strict": true,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "isolatedModules": false,
    "allowJs": false,
    "sourceMap": true,
    "noEmit": false,
    "jsx": "preserve",
    "incremental": true
  },
  "exclude": [
    "node_modules",
    "dist",
    "esm",
    ".next",
    "out",
    "types",
    "./README.md",
  ],
"include": [
    "./src",
  ]
}

Source code being tested

// date-fns-tz@1.3.8
import getTimezoneOffset from 'date-fns-tz/getTimezoneOffset';

export type LookupValues<T> = T[keyof T];

export const TIMEZONES = {
    PST: 'PST',
    EST: 'EST',
    CST: 'CST',
    MST: 'MST',
    JST: 'JST',
} as const;

export type SupportedTimezones = LookupValues<typeof TIMEZONES>;

export const TIMEZONE_TO_NON_DAYLIGHT_SAVINGS_DATE: Record<SupportedTimezones, Date> = {
    [TIMEZONES.PST]: new Date(2022, 0, 1, 0, 0, 0), // January 1, 2022 at 00:00:00
    [TIMEZONES.EST]: new Date(2022, 0, 1, 0, 0, 0), // January 1, 2022 at 00:00:00
    [TIMEZONES.CST]: new Date(2022, 0, 1, 0, 0, 0), // January 1, 2022 at 00:00:00
    [TIMEZONES.MST]: new Date(2022, 0, 1, 0, 0, 0), // January 1, 2022 at 00:00:00
    [TIMEZONES.JST]: new Date(2022, 0, 1, 0, 0, 0), // January 1, 2022 at 00:00:00
};

export const parsableTZOffset = (input: SupportedTimezones) => {
    // using NON_DAYLIGHT_SAVINGS_TIME_DATE is to default all regions to use Standard versus Daylight time
    const offset = getTimezoneOffset(input, TIMEZONE_TO_NON_DAYLIGHT_SAVINGS_DATE[input]) / MILLISECONDS_IN_HOUR;
    const isNegativeOffset = offset < 0;
    const absoluteOffset = Math.abs(offset);
    const hours = Math.floor(absoluteOffset);
    const minutes = Math.round((absoluteOffset % 1) * 60);
    const displayMinutes = String(minutes).padStart(2, '0');
    const displayHours = String(hours).padStart(2, '0');
    return isNegativeOffset ? `-${displayHours}${displayMinutes}` : `${displayHours}${displayMinutes}`;
};

Test Cases

describe(parsableTZOffset, () => {
        it.each<[SupportedTimezones, string]>([
            ['EST', '-0500'], // Eastern Standard Time (EST) offset from UTC: -5 hours
            ['CST', '-0600'], // Pacific Standard Time (PST) offset: -6 hours
            ['MST', '-0700'], // Mountain Standard Time (PST) offset: -7 hours
            ['PST', '-0800'], // Pacific Standard Time (PST) offset: -8 hours
            ['JST', '0900'], // Japanese Standard Time (PST) offset: +9 hours
        ])('should return the correct timezone offset for %s', (timezone, expectedOffset) => {
            expect(parsableTZOffset(timezone)).toBe(expectedOffset);
        });
    });

Error (When run through extension)

formatters > parsableTZOffset > should return the correct timezone offset for CST
-----
Error: expect(received).toBe(expected) // Object.is equality

Expected: "-0600"
Received: "NaNNaN"Jest

Passing (when run through console)

image

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

Successfully merging a pull request may close this issue.

5 participants