This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Precision option for rounding time passed on stats and dep in the com…
…pare function (#80) * chore(na): change default cache directory * chore(na): remove error changes * feat: add "precision" option for rounding mtime Some tools truncate mtime. (tar rounds to the second by default) Adding this option would allow support for environments that use such tools. # Conflicts: # src/index.js # src/options.json * chore(na): remove param reassign to original vars * test(na): some more tests for compare function * test(na): add tests for precision option
- Loading branch information
Showing
5 changed files
with
138 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,53 @@ | ||
const fs = require('fs'); | ||
|
||
const { webpack } = require('./helpers'); | ||
|
||
const mockCacheLoaderCompareFn = jest.fn(); | ||
const mockWebpackConfig = { | ||
loader: { | ||
options: { | ||
compare: () => { | ||
mockCacheLoaderCompareFn(); | ||
compare: (stats, dep) => { | ||
mockCacheLoaderCompareFn(stats, dep); | ||
return true; | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('compare option', () => { | ||
beforeEach(() => { | ||
mockCacheLoaderCompareFn.mockClear(); | ||
}); | ||
|
||
it('should call compare function', async () => { | ||
const testId = './basic/index.js'; | ||
await webpack(testId, mockWebpackConfig); | ||
await webpack(testId, mockWebpackConfig); | ||
expect(mockCacheLoaderCompareFn).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call compare function with 2 args', async () => { | ||
const testId = './basic/index.js'; | ||
await webpack(testId, mockWebpackConfig); | ||
await webpack(testId, mockWebpackConfig); | ||
expect(mockCacheLoaderCompareFn).toHaveBeenCalled(); | ||
expect(mockCacheLoaderCompareFn.mock.calls[0].length).toBe(2); | ||
}); | ||
|
||
it('should call compare function with correct args', async () => { | ||
const testId = './basic/index.js'; | ||
await webpack(testId, mockWebpackConfig); | ||
await webpack(testId, mockWebpackConfig); | ||
expect(mockCacheLoaderCompareFn).toHaveBeenCalled(); | ||
|
||
// eslint-disable-next-line | ||
const stats = mockCacheLoaderCompareFn.mock.calls[0][0]; | ||
// eslint-disable-next-line | ||
const dep = mockCacheLoaderCompareFn.mock.calls[0][1]; | ||
expect(stats).toBeDefined(); | ||
expect(stats instanceof fs.Stats); | ||
expect(dep).toBeDefined(); | ||
expect(dep.mtime).toBeDefined(); | ||
expect(dep.path).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const { webpack } = require('./helpers'); | ||
|
||
const mockCacheLoaderCompareFn = jest.fn(); | ||
const mockCacheLoaderCompareWithPrecisionFn = jest.fn(); | ||
const mockWebpackConfig = { | ||
loader: { | ||
options: { | ||
compare: (stats, dep) => { | ||
mockCacheLoaderCompareFn(stats, dep); | ||
return true; | ||
}, | ||
}, | ||
}, | ||
}; | ||
const mockWebpackWithPrecisionConfig = { | ||
loader: { | ||
options: { | ||
compare: (stats, dep) => { | ||
mockCacheLoaderCompareWithPrecisionFn(stats, dep); | ||
return true; | ||
}, | ||
precision: 1000, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('precision option', () => { | ||
beforeEach(() => { | ||
mockCacheLoaderCompareFn.mockClear(); | ||
mockCacheLoaderCompareWithPrecisionFn.mockClear(); | ||
}); | ||
|
||
it('should not apply precision', async () => { | ||
const testId = './basic/index.js'; | ||
await webpack(testId, mockWebpackConfig); | ||
mockCacheLoaderCompareFn.mockClear(); | ||
|
||
await webpack(testId, mockWebpackConfig); | ||
|
||
const pastPrecisionTime = mockCacheLoaderCompareFn.mock.calls[0][1].mtime; | ||
mockCacheLoaderCompareFn.mockClear(); | ||
|
||
await webpack(testId, mockWebpackConfig); | ||
expect(pastPrecisionTime).toBe( | ||
mockCacheLoaderCompareFn.mock.calls[0][1].mtime | ||
); | ||
}); | ||
|
||
it('should call compare with values after applying precision', async () => { | ||
const testId = './basic/index.js'; | ||
await webpack(testId, mockWebpackConfig); | ||
mockCacheLoaderCompareFn.mockClear(); | ||
await webpack(testId, mockWebpackConfig); | ||
await webpack(testId, mockWebpackWithPrecisionConfig); | ||
expect(mockCacheLoaderCompareFn.mock.calls[0][1].mtime).not.toBe( | ||
mockCacheLoaderCompareWithPrecisionFn.mock.calls[0][1].mtime | ||
); | ||
}); | ||
|
||
it('should apply precision dividing by the value', async () => { | ||
const testId = './basic/index.js'; | ||
await webpack(testId, mockWebpackConfig); | ||
mockCacheLoaderCompareFn.mockClear(); | ||
await webpack(testId, mockWebpackConfig); | ||
await webpack(testId, mockWebpackWithPrecisionConfig); | ||
|
||
const newMtime = | ||
mockCacheLoaderCompareWithPrecisionFn.mock.calls[0][1].mtime; | ||
const oldMtime = mockCacheLoaderCompareFn.mock.calls[0][1].mtime; | ||
expect(newMtime).toBe(Math.floor(oldMtime / 1000) * 1000); | ||
}); | ||
}); |