Use escape hatch for mocked modules

Reviewed By: davidaurelio

Differential Revision: D4869222

fbshipit-source-id: d2cf66296a26710630f0d0f6c8efc7ff3ed97bc5
This commit is contained in:
Christoph Pojer 2017-04-12 09:34:31 -07:00 committed by Facebook Github Bot
parent 2318321208
commit 0fa1c5f0f1
3 changed files with 16 additions and 15 deletions

View File

@ -12,8 +12,8 @@
jest.disableAutomock(); jest.disableAutomock();
jest.useRealTimers(); jest.useRealTimers();
const fetchMock = jest.fn(); const mockFetch = jest.fn();
jest.mock('node-fetch', () => fetchMock); jest.mock('node-fetch', () => mockFetch);
const {URIBasedGlobalTransformCache} = require('../GlobalTransformCache'); const {URIBasedGlobalTransformCache} = require('../GlobalTransformCache');
const FetchError = require('node-fetch/lib/fetch-error'); const FetchError = require('node-fetch/lib/fetch-error');
@ -82,19 +82,19 @@ describe('GlobalTransformCache', () => {
}); });
beforeEach(() => { beforeEach(() => {
fetchMock.mockReset(); mockFetch.mockReset();
}); });
it('fetches result', async () => { it('fetches result', async () => {
fetchMock.mockImplementation(defaultFetchMockImpl); mockFetch.mockImplementation(defaultFetchMockImpl);
const result = await URIBasedGlobalTransformCache const result = await URIBasedGlobalTransformCache
.fetchResultFromURI('http://globalcache.com/foo'); .fetchResultFromURI('http://globalcache.com/foo');
expect(result).toMatchSnapshot(); expect(result).toMatchSnapshot();
}); });
it('retries once on timeout', async () => { it('retries once on timeout', async () => {
fetchMock.mockImplementation(async uri => { mockFetch.mockImplementation(async uri => {
fetchMock.mockImplementation(defaultFetchMockImpl); mockFetch.mockImplementation(defaultFetchMockImpl);
throw new FetchError('timeout!', 'request-timeout'); throw new FetchError('timeout!', 'request-timeout');
}); });
const result = await URIBasedGlobalTransformCache const result = await URIBasedGlobalTransformCache

View File

@ -21,14 +21,14 @@ const imurmurhash = require('imurmurhash');
const crypto = require('crypto'); const crypto = require('crypto');
const jsonStableStringify = require('json-stable-stringify'); const jsonStableStringify = require('json-stable-stringify');
const memoryFS = new Map(); const mockFS = new Map();
jest.mock('fs', () => ({ jest.mock('fs', () => ({
readFileSync(filePath) { readFileSync(filePath) {
return memoryFS.get(filePath); return mockFS.get(filePath);
}, },
unlinkSync(filePath) { unlinkSync(filePath) {
memoryFS.delete(filePath); mockFS.delete(filePath);
}, },
readdirSync(dirPath) { readdirSync(dirPath) {
// Not required for it to work. // Not required for it to work.
@ -38,7 +38,7 @@ jest.mock('fs', () => ({
jest.mock('write-file-atomic', () => ({ jest.mock('write-file-atomic', () => ({
sync(filePath, data) { sync(filePath, data) {
memoryFS.set(filePath, data.toString()); mockFS.set(filePath, data.toString());
}, },
})); }));
@ -56,7 +56,7 @@ describe('TransformCache', () => {
beforeEach(() => { beforeEach(() => {
jest.resetModules(); jest.resetModules();
memoryFS.clear(); mockFS.clear();
TransformCache = require('../TransformCache'); TransformCache = require('../TransformCache');
}); });

View File

@ -19,9 +19,10 @@ jest
; ;
// This doesn't have state, and it's huge (Babel) so it's much faster to // This doesn't have state, and it's huge (Babel) so it's much faster to
// require it only once. // require it only once. The variable name is prefixed with "mock" as an escape-hatch
const extractDependencies = require('../../JSTransformer/worker/extract-dependencies'); // for babel-plugin-jest-hoist.
jest.mock('../../JSTransformer/worker/extract-dependencies', () => extractDependencies); const mockExtractDependencies = require('../../JSTransformer/worker/extract-dependencies');
jest.mock('../../JSTransformer/worker/extract-dependencies', () => mockExtractDependencies);
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
@ -119,7 +120,7 @@ describe('DependencyGraph', function() {
return new Promise(resolve => { return new Promise(resolve => {
let deps = {dependencies: [], dependencyOffsets: []}; let deps = {dependencies: [], dependencyOffsets: []};
if (!module.path.endsWith('.json')) { if (!module.path.endsWith('.json')) {
deps = extractDependencies(sourceCode); deps = mockExtractDependencies(sourceCode);
} }
resolve({...deps, code: sourceCode}); resolve({...deps, code: sourceCode});
}); });