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

[WIP] Remove usage of retainLines #5594

Merged
merged 19 commits into from
Feb 21, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions packages/jest-util/src/__tests__/get_callsite.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import fs from 'fs';
import SourceMap from 'source-map';
import getCallsite from '../get_callsite';

jest.mock('fs');

describe('getCallsite', () => {
beforeEach(() => {
fs.readFileSync = jest.fn();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you do jest.mock without a factory, all exports are mocks by default. So this beforeEach shouldn't be necessary

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do jest.clearMocks if needed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test also fails CI?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, not sure what I was thinking

});

test('without source map', () => {
const site = getCallsite(0);

expect(site.getFileName()).toEqual(__filename);
expect(site.getColumnNumber()).toEqual(expect.any(Number));
expect(site.getLineNumber()).toEqual(expect.any(Number));
expect(fs.readFileSync).not.toHaveBeenCalled();
});

test('ignores errors when fs throws', () => {
fs.readFileSync.mockImplementation(() => {
throw new Error('Mock error');
});

const site = getCallsite(0, {[__filename]: 'mockedSourceMapFile'});

expect(site.getFileName()).toEqual(__filename);
expect(site.getColumnNumber()).toEqual(expect.any(Number));
expect(site.getLineNumber()).toEqual(expect.any(Number));
expect(fs.readFileSync).toHaveBeenCalledWith('mockedSourceMapFile', 'utf8');
});

test('reads source map file to determine line and column', () => {
const sourceMapColumn = 1;
const sourceMapLine = 2;
SourceMap.SourceMapConsumer = class {
originalPositionFor(params) {
expect(params).toMatchObject({
column: expect.any(Number),
line: expect.any(Number),
});

return {
column: sourceMapColumn,
line: sourceMapLine,
};
}
};

const site = getCallsite(0, {[__filename]: 'mockedSourceMapFile'});

expect(site.getFileName()).toEqual(__filename);
expect(site.getColumnNumber()).toEqual(sourceMapColumn);
expect(site.getLineNumber()).toEqual(sourceMapLine);
expect(fs.readFileSync).toHaveBeenCalledWith('mockedSourceMapFile', 'utf8');
});
});