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

Correctly mock date.constructor #28

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: build

on: [push, pull_request]

concurrency:
group: ${{github.workflow}}-${{github.event_name}}-${{github.ref}}
cancel-in-progress: true

jobs:
test-and-build:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 13

- name: Install Dependencies
run: npm install

- name: Run build & test
run: |
npm run build

- name: Coveralls GitHub Action
uses: coverallsapp/github-action@v2.2.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ package-lock.json
__tests__
src
.babelrc
.travis.yml
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Mock `Date` when run unit test cases with jest. Make tests of `Date` easier.

[![Build Status](https://travis-ci.org/hustcc/jest-date-mock.svg?branch=master)](https://travis-ci.org/hustcc/jest-date-mock)
[![Build Status](https://github.com/hustcc/jest-date-mock/workflows/build/badge.svg)](https://github.com/hustcc/jest-date-mock/actions)
[![Coverage Status](https://coveralls.io/repos/github/hustcc/jest-date-mock/badge.svg?branch=master)](https://coveralls.io/github/hustcc/jest-date-mock)
[![npm](https://img.shields.io/npm/v/jest-date-mock.svg)](https://www.npmjs.com/package/jest-date-mock)
[![npm](https://img.shields.io/npm/dm/jest-date-mock.svg)](https://www.npmjs.com/package/jest-date-mock)
Expand Down
12 changes: 12 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ describe('jest-date-mock', () => {
// Date isEqual
expect(new Date()).toEqual(new Date(0));

// .constructor()
expect(+new Date(1000).constructor()).toBe(0);
expect(+new Date().constructor(10000)).toBe(10000);
expect(new Date().constructor(10000)).toBeInstanceOf(Date);


// +
expect(+new Date()).toBe(0);
expect(+new Date(10000)).toBe(10000);


// getTime
expect(new Date().getTime()).toBe(0);
expect(new Date(10000).getTime()).toBe(10000);

// instanceof
expect(new Date()).toBeInstanceOf(Date);
expect(new Date().constructor(10000)).toBeInstanceOf(Date);
hustcc marked this conversation as resolved.
Show resolved Hide resolved

// 2018-05-27 08:00:00
expect(new Date(Date.UTC(2018, 5, 27, 0, 0, 0)).getTime()).toBe(1530057600000);
Expand Down Expand Up @@ -91,6 +99,9 @@ describe('jest-date-mock', () => {
expect(+new Date() - now).toBe(0);
advanceBy();
expect(+new Date() - now).toBe(0);

advanceBy(3000);
expect(+new Date().constructor() - now).toBe(3000); // .constructor()
});

test('clear', () => {
Expand All @@ -99,6 +110,7 @@ describe('jest-date-mock', () => {

advanceTo();
expect(+new Date()).toBe(0);
expect(+new Date().constructor()).toBe(0);
});

test('usage', () => {
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"typings": "lib/index.d.ts",
"scripts": {
"test": "jest --no-cache",
"build": "babel src --out-dir lib --copy-files && npm run test",
"coveralls": "cat ./coverage/lcov.info | coveralls"
"build": "babel src --out-dir lib --copy-files && npm run test"
},
"repository": {
"type": "git",
Expand All @@ -33,7 +32,6 @@
"@babel/preset-env": "^7.7.7",
"babel-jest": "^24.9.0",
"babel-plugin-version": "^0.2.2",
"coveralls": "^3.0.0",
"jest": "^24.9.0"
},
"jest": {
Expand Down
1 change: 1 addition & 0 deletions src/mockDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const mockDateClass = D => {
const dateArgs = args.length === 0 ? [mockNow()] : args;
const instance = new D(...dateArgs);
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
instance.constructor = Date;
return instance;
}

Expand Down