Skip to content

Commit

Permalink
Removing automatically reset modules between test run
Browse files Browse the repository at this point in the history
Reviewed By: bestander

Differential Revision: D3886125

fbshipit-source-id: c8f47c3466add1e2c89649a1c6f47b01f0d7a89e
  • Loading branch information
Cristian Carlesso authored and Facebook Github Bot 3 committed Sep 19, 2016
1 parent d41c395 commit 7b2080e
Show file tree
Hide file tree
Showing 9 changed files with 665 additions and 605 deletions.
956 changes: 480 additions & 476 deletions Libraries/Animated/src/__tests__/Animated-test.js

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion local-cli/link/__tests__/getProjectDependencies.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

jest.autoMockOff();
Expand All @@ -6,7 +15,9 @@ const getProjectDependencies = require('../getProjectDependencies');
const path = require('path');

describe('getProjectDependencies', () => {

beforeEach(() => {
jest.resetModules();
});
it('should return an array of project dependencies', () => {
jest.setMock(
path.join(process.cwd(), './package.json'),
Expand Down
1 change: 1 addition & 0 deletions local-cli/link/__tests__/link.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jest.setMock(

describe('link', () => {
beforeEach(() => {
jest.resetModules();
delete require.cache[require.resolve('../link')];
log.level = 'silent';
});
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"jestSupport/env.js"
],
"timers": "fake",
"resetModules": true,
"moduleNameMapper": {
"^image![a-zA-Z0-9$_-]+$": "GlobalImageStub",
"^[./a-zA-Z0-9$_-]+\\.png$": "RelativeImageStub"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('code transformation worker:', () => {

let extractDependencies, transform;
beforeEach(() => {
jest.resetModules();
({transformCode} = require('..'));
extractDependencies =
require('../extract-dependencies').mockReturnValue({});
Expand Down Expand Up @@ -58,18 +59,22 @@ describe('code transformation worker:', () => {
});
});

it('removes the leading assignment to `module.exports` before passing on the result if the file is a JSON file, even if minified', done => {
const result = {
code: 'p.exports={a:1,b:2}',
};
transform.mockImplementation((_, callback) =>
callback(null, result));
it(
'removes the leading assignment to `module.exports` before passing ' +
'on the result if the file is a JSON file, even if minified',
done => {
const result = {
code: 'p.exports={a:1,b:2}',
};
transform.mockImplementation((_, callback) =>
callback(null, result));

transformCode(transform, 'aribtrary/file.json', 'b', {}, (_, data) => {
expect(data.code).toBe('{a:1,b:2}');
done();
});
});
transformCode(transform, 'aribtrary/file.json', 'b', {}, (_, data) => {
expect(data.code).toBe('{a:1,b:2}');
done();
});
}
);

it('removes shebang when present', done => {
const shebang = '#!/usr/bin/env node';
Expand Down Expand Up @@ -110,35 +115,51 @@ describe('code transformation worker:', () => {
});
});

it('uses `dependencies` and `dependencyOffsets` provided by `extractDependencies` for the result', done => {
const dependencyData = {
dependencies: ['arbitrary', 'list', 'of', 'dependencies'],
dependencyOffsets: [12, 119, 185, 328, 471],
};
extractDependencies.mockReturnValue(dependencyData);

transformCode(transform, 'filename', 'code', {}, (_, data) => {
expect(data).toEqual(objectContaining(dependencyData));
done();
});
});
it(
'uses `dependencies` and `dependencyOffsets` ' +
'provided by `extractDependencies` for the result',
done => {
const dependencyData = {
dependencies: ['arbitrary', 'list', 'of', 'dependencies'],
dependencyOffsets: [12, 119, 185, 328, 471],
};
extractDependencies.mockReturnValue(dependencyData);

transformCode(transform, 'filename', 'code', {}, (_, data) => {
expect(data).toEqual(objectContaining(dependencyData));
done();
});
}
);

it('does not extract requires if files are marked as "extern"', done => {
transformCode(transform, 'filename', 'code', {extern: true}, (_, {dependencies, dependencyOffsets}) => {
expect(extractDependencies).not.toBeCalled();
expect(dependencies).toEqual([]);
expect(dependencyOffsets).toEqual([]);
done();
});
transformCode(
transform,
'filename',
'code',
{extern: true},
(_, {dependencies, dependencyOffsets}) => {
expect(extractDependencies).not.toBeCalled();
expect(dependencies).toEqual([]);
expect(dependencyOffsets).toEqual([]);
done();
}
);
});

it('does not extract requires of JSON files', done => {
transformCode(transform, 'arbitrary.json', '{"arbitrary":"json"}', {}, (_, {dependencies, dependencyOffsets}) => {
expect(extractDependencies).not.toBeCalled();
expect(dependencies).toEqual([]);
expect(dependencyOffsets).toEqual([]);
done();
});
transformCode(
transform,
'arbitrary.json',
'{"arbitrary":"json"}',
{},
(_, {dependencies, dependencyOffsets}) => {
expect(extractDependencies).not.toBeCalled();
expect(dependencies).toEqual([]);
expect(dependencyOffsets).toEqual([]);
done();
}
);
});
});

Expand Down
101 changes: 54 additions & 47 deletions packager/react-packager/src/Server/__tests__/Server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let FileWatcher;
describe('processRequest', () => {
let SourceMapConsumer, Bundler, Server, AssetServer, Promise;
beforeEach(() => {
jest.resetModules();
SourceMapConsumer = require('source-map').SourceMapConsumer;
Bundler = require('../../Bundler');
Server = require('../');
Expand Down Expand Up @@ -253,52 +254,55 @@ describe('processRequest', () => {
jest.runAllTicks();
});

it('does not rebuild the bundles that contain a file when that file is changed, even when hot loading is enabled', () => {
const bundleFunc = jest.fn();
bundleFunc
.mockReturnValueOnce(
Promise.resolve({
getSource: () => 'this is the first source',
getSourceMap: () => {},
getEtag: () => () => 'this is an etag',
})
)
.mockReturnValue(
Promise.resolve({
getSource: () => 'this is the rebuilt source',
getSourceMap: () => {},
getEtag: () => () => 'this is an etag',
})
);

Bundler.prototype.bundle = bundleFunc;

server = new Server(options);
server.setHMRFileChangeListener(() => {});

requestHandler = server.processRequest.bind(server);

makeRequest(requestHandler, 'mybundle.bundle?runModule=true')
.done(response => {
expect(response.body).toEqual('this is the first source');
expect(bundleFunc.mock.calls.length).toBe(1);
});

jest.runAllTicks();

triggerFileChange('all','path/file.js', options.projectRoots[0]);
jest.runAllTimers();
jest.runAllTicks();

expect(bundleFunc.mock.calls.length).toBe(1);
server.setHMRFileChangeListener(null);

makeRequest(requestHandler, 'mybundle.bundle?runModule=true')
.done(response => {
expect(response.body).toEqual('this is the rebuilt source');
expect(bundleFunc.mock.calls.length).toBe(2);
});
jest.runAllTicks();
it(
'does not rebuild the bundles that contain a file ' +
'when that file is changed, even when hot loading is enabled',
() => {
const bundleFunc = jest.fn();
bundleFunc
.mockReturnValueOnce(
Promise.resolve({
getSource: () => 'this is the first source',
getSourceMap: () => {},
getEtag: () => () => 'this is an etag',
})
)
.mockReturnValue(
Promise.resolve({
getSource: () => 'this is the rebuilt source',
getSourceMap: () => {},
getEtag: () => () => 'this is an etag',
})
);

Bundler.prototype.bundle = bundleFunc;

server = new Server(options);
server.setHMRFileChangeListener(() => {});

requestHandler = server.processRequest.bind(server);

makeRequest(requestHandler, 'mybundle.bundle?runModule=true')
.done(response => {
expect(response.body).toEqual('this is the first source');
expect(bundleFunc.mock.calls.length).toBe(1);
});

jest.runAllTicks();

triggerFileChange('all','path/file.js', options.projectRoots[0]);
jest.runAllTimers();
jest.runAllTicks();

expect(bundleFunc.mock.calls.length).toBe(1);
server.setHMRFileChangeListener(null);

makeRequest(requestHandler, 'mybundle.bundle?runModule=true')
.done(response => {
expect(response.body).toEqual('this is the rebuilt source');
expect(bundleFunc.mock.calls.length).toBe(2);
});
jest.runAllTicks();
});
});

Expand Down Expand Up @@ -382,7 +386,10 @@ describe('processRequest', () => {

server.processRequest(req, res);
jest.runAllTimers();
expect(AssetServer.prototype.get).toBeCalledWith('imgs/主页/logo.png', undefined);
expect(AssetServer.prototype.get).toBeCalledWith(
'imgs/\u{4E3B}\u{9875}/logo.png',
undefined
);
expect(res.setHeader).toBeCalledWith('Cache-Control', 'max-age=31536000');
expect(res.end).toBeCalledWith('i am image');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('FileWatcher', () => {
let config;

beforeEach(() => {
jest.resetModules();
const sane = require('sane');
WatchmanWatcher = sane.WatchmanWatcher;
WatchmanWatcher.prototype.once.mockImplementation(
Expand Down
Loading

0 comments on commit 7b2080e

Please sign in to comment.