forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: write snapshots to disk even if FS is mocked
Fixes jestjs#5270
- Loading branch information
Showing
8 changed files
with
117 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`store snapshot even if fs is mocked 1`] = ` | ||
"Test Suites: 1 passed, 1 total | ||
Tests: 1 passed, 1 total | ||
Snapshots: 1 written, 1 total | ||
Time: <<REPLACED>> | ||
Ran all test suites." | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
const rimraf = require('rimraf'); | ||
const path = require('path'); | ||
const {extractSummary} = require('../Utils'); | ||
const runJest = require('../runJest'); | ||
|
||
const DIR = path.resolve(__dirname, '../snapshot-mock-fs'); | ||
const snapshotDir = path.resolve(DIR, '__tests__/__snapshots__'); | ||
const snapshotFile = path.resolve(snapshotDir, 'snapshot.test.js.snap'); | ||
|
||
beforeEach(() => rimraf.sync(snapshotDir)); | ||
afterAll(() => rimraf.sync(snapshotDir)); | ||
|
||
test('store snapshot even if fs is mocked', () => { | ||
const {json, status, stderr} = runJest.json(DIR, ['--ci=false']); | ||
|
||
expect(status).toBe(0); | ||
expect(json.numTotalTests).toBe(1); | ||
expect(json.numPassedTests).toBe(1); | ||
|
||
expect(stderr).toMatch('1 snapshot written from 1 test suite.'); | ||
expect(extractSummary(stderr).summary).toMatchSnapshot(); | ||
|
||
// $FlowFixMe dynamic require | ||
const content = require(snapshotFile); | ||
expect(content['snapshot 1']).toBe(` | ||
Object { | ||
"foo": "bar", | ||
} | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails oncall+jsinfra | ||
*/ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
|
||
fs.writeFileSync = jest.fn(); | ||
|
||
test('snapshot', () => { | ||
const thing = {foo: 'bar'}; | ||
|
||
expect(thing).toMatchSnapshot(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
// This file exists so that mocks in userland does not affect snapshots | ||
|
||
import fs from 'fs'; | ||
|
||
export const boundReadFile = fs.readFileSync.bind(fs); | ||
export const boundWriteFile = fs.writeFileSync.bind(fs); | ||
export const boundExistsSync = fs.existsSync.bind(fs); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters