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

Comments in test file throws exception when writing inline snapshots #1748

Closed
MrOrz opened this issue Jun 23, 2020 · 6 comments
Closed

Comments in test file throws exception when writing inline snapshots #1748

MrOrz opened this issue Jun 23, 2020 · 6 comments

Comments

@MrOrz
Copy link

MrOrz commented Jun 23, 2020

🐛 Bug Report

When a test file

  • has comments
  • has empty toMatchInlineSnapshot()

Running jest will fail when prettier tries to prettify the file with updated snapshot.

Test file:

//
it('comment + inlinesnapshot breaks jest', () => {
  expect(true).toMatchInlineSnapshot();
});

Test result:

$ npm t -- typescript.test.ts

> snapshot-comment-error@1.0.0 test /Users/johnsonliang/workspace/snapshot-comment-error
> jest "typescript.test.ts"

 FAIL  ./typescript.test.ts
  ● Test suite failed to run

    TypeError: Property comments[0] of File expected node to be of a type ["Comment"] but instead got "Line"

      at validate (node_modules/@babel/types/lib/definitions/utils.js:132:11)
      at Object.validator [as validate] (node_modules/@babel/types/lib/definitions/utils.js:103:7)
      at validateField (node_modules/@babel/types/lib/validators/validate.js:24:9)
      at validate (node_modules/@babel/types/lib/validators/validate.js:17:3)
      at builder (node_modules/@babel/types/lib/builders/builder.js:38:27)
      at File (node_modules/@babel/types/lib/builders/generated/index.js:318:31)
      at Object.parse (node_modules/prettier/index.js:11370:19)
      at coreFormat (node_modules/prettier/index.js:14784:25)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.13 s, estimated 3 s
Ran all test suites matching /typescript.test.ts/i.
npm ERR! Test failed.  See above for more details.

To Reproduce

Steps to reproduce the behavior:

$ git clone git@github.com:MrOrz/tsjest-comment-error.git
$ cd tsjest-comment-error
$ npm i
$ npm t

Observations:

  • The repo includes javascript.test.js and typescript.test.ts with identical code.
    • It's only reproducible in typescript files: When running npm t, javascript.test.js can have its snapshot successfully updated, while typescript.test.ts fails.
  • Moving the comment in typescript.test.ts to any location can still trigger the exception
  • The test passes if and only if we remove all comment lines from typescript.test.ts

Expected behavior

Snapshot in test files gets updated.

Link to repo (highly encouraged)

https://github.com/MrOrz/tsjest-comment-error

Debug log:

# content of ts-jest.log :
{"context":{"allowJs":false,"logLevel":20,"namespace":"jest-preset","package":"ts-jest","version":"26.1.1"},"message":"creating jest presets not handling JavaScript files","sequence":1,"time":"2020-06-23T03:51:05.137Z"}
{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","version":"26.1.1"},"message":"creating Importer singleton","sequence":2,"time":"2020-06-23T03:51:07.556Z"}
{"context":{"allowJs":false,"logLevel":20,"namespace":"jest-preset","package":"ts-jest","version":"26.1.1"},"message":"creating jest presets not handling JavaScript files","sequence":3,"time":"2020-06-23T03:51:07.563Z"}
{"context":{"actualVersion":"26.0.1","expectedVersion":">=26 <27","logLevel":20,"namespace":"versions","package":"ts-jest","version":"26.1.1"},"message":"checking version of jest: OK","sequence":4,"time":"2020-06-23T03:51:07.565Z"}
{"context":{"baseOptions":{},"logLevel":20,"namespace":"jest-transformer","package":"ts-jest","transformerId":1,"version":"26.1.1"},"message":"created new transformer","sequence":5,"time":"2020-06-23T03:51:07.565Z"}
{"context":{"fileName":"/Users/johnsonliang/workspace/snapshot-comment-error/typescript.test.ts","logLevel":20,"namespace":"jest-transformer","package":"ts-jest","transformOptions":{"config":{"automock":false,"cache":true,"cacheDirectory":"/private/var/folders/r4/whpnz2j55p72y0brmr1f3rdr0000gn/T/jest_dx","clearMocks":false,"coveragePathIgnorePatterns":["/node_modules/"],"cwd":"/Users/johnsonliang/workspace/snapshot-comment-error","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{"computeSha1":false,"throwOnModuleCollision":false},"moduleDirectories":["node_modules"],"moduleFileExtensions":["js","json","jsx","ts","tsx","node"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"name":"a715305738be4fcdea2147c3ce6123dc","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/Users/johnsonliang/workspace/snapshot-comment-error","roots":["/Users/johnsonliang/workspace/snapshot-comment-error"],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"snapshotSerializers":[],"testEnvironment":"/Users/johnsonliang/workspace/snapshot-comment-error/node_modules/jest-environment-node/build/index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["**/__tests__/**/*.[jt]s?(x)","**/?(*.)+(spec|test).[tj]s?(x)"],"testPathIgnorePatterns":["/node_modules/"],"testRegex":[],"testRunner":"/Users/johnsonliang/workspace/snapshot-comment-error/node_modules/jest-jasmine2/build/index.js","testURL":"http://localhost","timers":"real","transform":[["^.+\\.tsx?$","/Users/johnsonliang/workspace/snapshot-comment-error/node_modules/ts-jest/dist/index.js",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]},"instrument":false,"rootDir":"/Users/johnsonliang/workspace/snapshot-comment-error","supportsDynamicImport":false,"supportsStaticESM":false},"transformerId":1,"version":"26.1.1"},"message":"computing cache key for /Users/johnsonliang/workspace/snapshot-comment-error/typescript.test.ts","sequence":6,"time":"2020-06-23T03:51:07.566Z"}
{"context":{"logLevel":30,"namespace":"jest-transformer","package":"ts-jest","transformerId":1,"version":"26.1.1"},"message":"no matching config-set found, creating a new one","sequence":7,"time":"2020-06-23T03:51:07.566Z"}
{"context":{"config":{"automock":false,"cache":true,"cacheDirectory":"/private/var/folders/r4/whpnz2j55p72y0brmr1f3rdr0000gn/T/jest_dx","clearMocks":false,"coveragePathIgnorePatterns":["/node_modules/"],"cwd":"/Users/johnsonliang/workspace/snapshot-comment-error","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{"computeSha1":false,"throwOnModuleCollision":false},"moduleDirectories":["node_modules"],"moduleFileExtensions":["js","json","jsx","ts","tsx","node"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"name":"a715305738be4fcdea2147c3ce6123dc","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/Users/johnsonliang/workspace/snapshot-comment-error","roots":["/Users/johnsonliang/workspace/snapshot-comment-error"],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"snapshotSerializers":[],"testEnvironment":"/Users/johnsonliang/workspace/snapshot-comment-error/node_modules/jest-environment-node/build/index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["**/__tests__/**/*.[jt]s?(x)","**/?(*.)+(spec|test).[tj]s?(x)"],"testPathIgnorePatterns":["/node_modules/"],"testRegex":[],"testRunner":"/Users/johnsonliang/workspace/snapshot-comment-error/node_modules/jest-jasmine2/build/index.js","testURL":"http://localhost","timers":"real","transform":[["^.+\\.tsx?$","/Users/johnsonliang/workspace/snapshot-comment-error/node_modules/ts-jest/dist/index.js",{}]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]},"logLevel":20,"namespace":"backports","package":"ts-jest","transformerId":1,"version":"26.1.1"},"message":"backporting config","sequence":8,"time":"2020-06-23T03:51:07.566Z"}

envinfo

System:
    OS: Mac OSX

Npm packages:
    jest: 26.0.1
    ts-jest: 26.1.1
    typescript: 3.9.5
    prettier: 2.0.5
    babel(optional): 
@MrOrz MrOrz added Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Jun 23, 2020
@MrOrz
Copy link
Author

MrOrz commented Jun 23, 2020

As a workaround, I use multiline strings as a replacement of multiline comments in my test files. Works like a charm lol

#wisdomFromPython

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jun 23, 2020

The stacktrace throws out is the one from Babel core. This is not ts-jest issue :)

You should create an issue for jest team.

@ahnpnl ahnpnl closed this as completed Jun 23, 2020
@ahnpnl ahnpnl added 💬 Question and removed Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Jun 23, 2020
@MrOrz
Copy link
Author

MrOrz commented Jun 23, 2020

You should create an issue for jest team.

Thanks for the quick response!

I am afraid that it is not jest's issue, because it is not reproducible in jest & JS files. Only the combination of ts-jest & typescript test files triggers this issue.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jun 23, 2020

Updated: I checked your repo, actually you don't use babel-jest. But still the error is caused by babel-jest cannot handle the compiled output. This is the compiled output of typescript.test.ts

//
it('comment + inlinesnapshot breaks jest', function () {
    expect(true).toMatchInlineSnapshot();
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiL1VzZXJzL2Fobi90c2plc3QtY29tbWVudC1lcnJvci90eXBlc2NyaXB0LnRlc3QudHMiLCJzb3VyY2VzIjpbIi9Vc2Vycy9haG4vdHNqZXN0LWNvbW1lbnQtZXJyb3IvdHlwZXNjcmlwdC50ZXN0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLEVBQUU7QUFDRixFQUFFLENBQUMsc0NBQXNDLEVBQUU7SUFDekMsTUFBTSxDQUFDLElBQUksQ0FBQyxDQUFDLHFCQUFxQixFQUFFLENBQUM7QUFDdkMsQ0FBQyxDQUFDLENBQUMiLCJzb3VyY2VzQ29udGVudCI6WyIvL1xuaXQoJ2NvbW1lbnQgKyBpbmxpbmVzbmFwc2hvdCBicmVha3MgamVzdCcsICgpID0

as you can see the compile output is similar to the original code. The error stack points to jest because it somehow cannot handle this output

@meyer
Copy link

meyer commented Jun 28, 2020

I’m running into this same issue. Looks like it’s being tracked here: jestjs/jest#10208

@meyer
Copy link

meyer commented Jun 28, 2020

here’s the fix: babel/babel#11752

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

No branches or pull requests

3 participants