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

Update documentation on automocking #5630

Merged
merged 12 commits into from
Feb 21, 2018
Prev Previous commit
Next Next commit
Add tests for genMockFromModule
  • Loading branch information
czystyl committed Feb 20, 2018
commit 9718940d8689121fc519ad74146430544561992f
16 changes: 16 additions & 0 deletions examples/automatic_mocks/__tests__/genMockFromModule.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2004-present Facebook. All Rights Reserved.

//we need to disable automocking because is enabled on config in this example
jest.disableAutomock();

import utils from '../utilsMocked';

test('if exist additional implementation', () => {
console.log(utils.isAuthorized.getMockImplementation.getMockImplementation);
// expect(utils.authorized('wizzard')).toBeTruthy();
});

test('mocked implementation', () => {
expect(utils.authorize._isMockFunction).toBe(true);
expect(utils.isAuthorized._isMockFunction).toBe(true);
});
41 changes: 41 additions & 0 deletions examples/automatic_mocks/utilsMocked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2004-present Facebook. All Rights Reserved.

const utils = jest.genMockFromModule('./utils');
// All implementation from original urils module is now mocked

utils.isAuthorized = secret => {
console.log(123123123123);
return secret === 'not_wizzard';
};

module.exports = utils;

//
// const fs = jest.genMockFromModule('fs');
//
// // This is a custom function that our tests can use during setup to specify
// // what the files on the "mock" filesystem should look like when any of the
// // `fs` APIs are used.
// let mockFiles = Object.create(null);
// function __setMockFiles(newMockFiles) {
// mockFiles = Object.create(null);
// for (const file in newMockFiles) {
// const dir = path.dirname(file);
//
// if (!mockFiles[dir]) {
// mockFiles[dir] = [];
// }
// mockFiles[dir].push(path.basename(file));
// }
// }
//
// // A custom version of `readdirSync` that reads from the special mocked out
// // file list set via __setMockFiles
// function readdirSync(directoryPath) {
// return mockFiles[directoryPath] || [];
// }
//
// fs.__setMockFiles = __setMockFiles;
// fs.readdirSync = readdirSync;
//
// module.exports = fs;