question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Comments in test file throws exception when writing inline snapshots

See original GitHub issue

🐛 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
    • Not only at the end of the file. Maybe a different issue from #1730 .
  • 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): 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
meyercommented, Jun 28, 2020
1reaction
meyercommented, Jun 28, 2020

I’m running into this same issue. Looks like it’s being tracked here: https://github.com/facebook/jest/issues/10208

Read more comments on GitHub >

github_iconTop Results From Across the Web

jest is not creating files for snapshots it add it inline to the ...
This method writes the content of the snapshot directly into the test file: https://jestjs.io/docs/snapshot-testing#inline-snapshots.
Read more >
Snapshots on Exceptions while debugging with IntelliTrace
The first time an exception is thrown, IntelliTrace will take a snapshot and mark it accordingly. If this exception is caught and then...
Read more >
Expect
toThrowErrorMatchingInlineSnapshot to test that a function throws an error matching the most recent snapshot when it is called. Jest adds the ...
Read more >
Changelog
Cypress has always recommended writing tests in a clean context. ... Cypress now throws an error if any Cypress commands are invoked from...
Read more >
Introduction to Jest Snapshot Testing in Vue.js
On subsequent runs of your test suite, the DOM that's constructed gets compared to these snapshots. Since you may have changed your code,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found