Skip to content

Commit

Permalink
Use shell.exec mock
Browse files Browse the repository at this point in the history
I initially try to mock the whole shelljs. But it returns error
shell.exec is not a function when i try to provide the mockResolvedValue
I think it is because of the inner code of shelljs who run a forEach to
require each of its method which make it a promise. I tried moving the
jest.mock inside beforeAll and also adding babel-dynamic-import but it did
not solve the problem. In the end, I decided to just mock shelljs.exec since
it is the only function used anyway
  • Loading branch information
fiennyangeln committed Oct 14, 2018
1 parent e3f3ca3 commit b38dd21
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions v1/lib/core/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,38 @@ describe('utils', () => {
fs.unlinkSync(tempFilePath);

// test renaming and moving file
const tempFilePath2 = path.join(__dirname, '__fixtures__', '.temp2');
fs.writeFileSync(tempFilePath2, 'Lorem ipsum :)');

shell.exec(`git add ${tempFilePath2}`);
shell.exec(`git commit -m "Create new file" --only ${tempFilePath2}`);

const createTime = utils.getGitLastUpdated(tempFilePath2);
expect(typeof createTime).toBe('string');

// rename / move the file
fs.mkdirSync(path.join(__dirname, '__fixtures__', 'test'));
const tempFilePath2 = path.join(__dirname, '__fixtures__', '.temp2');
const tempFilePath3 = path.join(
__dirname,
'__fixtures__',
'test',
'.temp3',
);
shell.exec(`git mv ${tempFilePath2} ${tempFilePath3}`);
shell.exec(`git commit -m "Rename the file"`);

// create new file
shell.exec = jest.fn(() => ({
stdout:
'1539502055\n' +
'\n' +
' create mode 100644 v1/lib/core/__tests__/__fixtures__/.temp2\n',
}));
const createTime = utils.getGitLastUpdated(tempFilePath2);
expect(typeof createTime).toBe('string');

// rename / move the file
shell.exec = jest.fn(() => ({
stdout:
'1539502056\n' +
'\n' +
' rename v1/lib/core/__tests__/__fixtures__/{.temp2 => test/.temp3} (100%)\n' +
'1539502055\n' +
'\n' +
' create mode 100644 v1/lib/core/__tests__/__fixtures__/.temp2\n',
}));
const lastUpdateTime = utils.getGitLastUpdated(tempFilePath3);
// should only consider file content change
expect(lastUpdateTime).toEqual(createTime);

fs.unlinkSync(tempFilePath3);
fs.rmdirSync(path.join(__dirname, '__fixtures__', 'test'));
shell.exec(`git reset HEAD^^`);
});

test('idx', () => {
Expand Down

0 comments on commit b38dd21

Please sign in to comment.