Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
jest-date-mock
Advanced tools
Readme
Mock
Date
when run unit test cases with jest. Make tests ofDate
easier.
- jest-random-mock Mock
Math.random
in jest, with deterministic random generator.- jest-canvas-mock Mock
canvas
when run unit test cases with jest.
This should only be installed as a development dependency (devDependencies
) as it is only designed for testing.
npm i --save-dev jest-date-mock
In your package.json
under the jest
, create a setupFiles
array and add jest-date-mock
to the array.
{
"jest": {
"setupFiles": ["jest-date-mock"]
}
}
If you already have a setupFiles
attribute you can also append jest-date-mock
to the array.
{
"jest": {
"setupFiles": ["./__setups__/other.js", "jest-date-mock"]
}
}
More about in configuration section.
Alternatively you can create a new setup file which then requires this module or
add the require
statement to an existing setup file.
__setups__/date.js
import 'jest-date-mock';
// or
require('jest-date-mock');
Add that file to your setupFiles
array:
"jest": {
"setupFiles": [
"./__setups__/date.js"
]
}
Use the only
3 api
for test cases.
advanceBy(ms)
: advance date timestamp by ms
.advanceTo([timestamp])
: reset date to timestamp
, default to 0
.clear()
: shut down the mock system.import { advanceBy, advanceTo, clear } from 'jest-date-mock';
test('usage', () => {
advanceTo(new Date(2018, 5, 27, 0, 0, 0)); // reset to date time.
const now = Date.now();
advanceBy(3000); // advance time 3 seconds
expect(+new Date() - now).toBe(3000);
advanceBy(-1000); // advance time -1 second
expect(+new Date() - now).toBe(2000);
clear();
Date.now(); // will got current timestamp
});
More sample code here.
Also, add an API Date.current()
to get the actual current timestamp.
import { advanceBy, advanceTo, clear } from 'jest-date-mock';
advanceTo(0); // reset to timestamp = 0
Date.now(); // will got 0
Date.current(); // will got the actual timestamp.
MIT@hustcc.
FAQs
Mock `window.Date` when run unit test cases with jest. Make tests of `Date` easier.
The npm package jest-date-mock receives a total of 251,805 weekly downloads. As such, jest-date-mock popularity was classified as popular.
We found that jest-date-mock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.