|
| 1 | +'use strict'; |
| 2 | +process.env.NODE_TEST_KNOWN_GLOBALS = 0; |
| 3 | +require('../common'); |
| 4 | + |
| 5 | +const assert = require('node:assert'); |
| 6 | +const { it, describe } = require('node:test'); |
| 7 | + |
| 8 | +describe('Mock Timers Date Test Suite', () => { |
| 9 | + it('should return the initial UNIX epoch if not specified', (t) => { |
| 10 | + t.mock.timers.enable({ apis: ['Date'] }); |
| 11 | + const date = new Date(); |
| 12 | + assert.strictEqual(date.getTime(), 0); |
| 13 | + assert.strictEqual(Date.now(), 0); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should throw an error if setTime is called without enabling timers', (t) => { |
| 17 | + assert.throws( |
| 18 | + () => { |
| 19 | + t.mock.timers.setTime(100); |
| 20 | + }, |
| 21 | + { code: 'ERR_INVALID_STATE' } |
| 22 | + ); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should throw an error if epoch passed to enable is not valid', (t) => { |
| 26 | + assert.throws( |
| 27 | + () => { |
| 28 | + t.mock.timers.enable({ now: -1 }); |
| 29 | + }, |
| 30 | + { code: 'ERR_INVALID_ARG_VALUE' } |
| 31 | + ); |
| 32 | + |
| 33 | + assert.throws( |
| 34 | + () => { |
| 35 | + t.mock.timers.enable({ now: 'string' }); |
| 36 | + }, |
| 37 | + { code: 'ERR_INVALID_ARG_TYPE' } |
| 38 | + ); |
| 39 | + |
| 40 | + assert.throws( |
| 41 | + () => { |
| 42 | + t.mock.timers.enable({ now: NaN }); |
| 43 | + }, |
| 44 | + { code: 'ERR_INVALID_ARG_VALUE' } |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should replace the original Date with the mocked one', (t) => { |
| 49 | + t.mock.timers.enable({ apis: ['Date'] }); |
| 50 | + assert.ok(Date.isMock); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return the ticked time when calling Date.now after tick', (t) => { |
| 54 | + t.mock.timers.enable({ apis: ['Date'] }); |
| 55 | + const time = 100; |
| 56 | + t.mock.timers.tick(time); |
| 57 | + assert.strictEqual(Date.now(), time); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return the Date as string when calling it as a function', (t) => { |
| 61 | + t.mock.timers.enable({ apis: ['Date'] }); |
| 62 | + const returned = Date(); |
| 63 | + // Matches the format: 'Mon Jan 01 1970 00:00:00' |
| 64 | + // We don't care about the date, just the format |
| 65 | + assert.ok(/\w{3}\s\w{3}\s\d{1,2}\s\d{2,4}\s\d{1,2}:\d{2}:\d{2}/.test(returned)); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should return the date with different argument calls', (t) => { |
| 69 | + t.mock.timers.enable({ apis: ['Date'] }); |
| 70 | + assert.strictEqual(new Date(0).getTime(), 0); |
| 71 | + assert.strictEqual(new Date(100).getTime(), 100); |
| 72 | + assert.strictEqual(new Date('1970-01-01T00:00:00.000Z').getTime(), 0); |
| 73 | + assert.strictEqual(new Date(1970, 0).getFullYear(), 1970); |
| 74 | + assert.strictEqual(new Date(1970, 0).getMonth(), 0); |
| 75 | + assert.strictEqual(new Date(1970, 0, 1).getDate(), 1); |
| 76 | + assert.strictEqual(new Date(1970, 0, 1, 11).getHours(), 11); |
| 77 | + assert.strictEqual(new Date(1970, 0, 1, 11, 10).getMinutes(), 10); |
| 78 | + assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45).getSeconds(), 45); |
| 79 | + assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45, 898).getMilliseconds(), 898); |
| 80 | + assert.strictEqual(new Date(1970, 0, 1, 11, 10, 45, 898).toDateString(), 'Thu Jan 01 1970'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should return native code when calling Date.toString', (t) => { |
| 84 | + t.mock.timers.enable({ apis: ['Date'] }); |
| 85 | + assert.strictEqual(Date.toString(), 'function Date() { [native code] }'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should start with a custom epoch if the second argument is specified', (t) => { |
| 89 | + t.mock.timers.enable({ apis: ['Date'], now: 100 }); |
| 90 | + const date1 = new Date(); |
| 91 | + assert.strictEqual(date1.getTime(), 100); |
| 92 | + |
| 93 | + t.mock.timers.reset(); |
| 94 | + t.mock.timers.enable({ apis: ['Date'], now: new Date(200) }); |
| 95 | + const date2 = new Date(); |
| 96 | + assert.strictEqual(date2.getTime(), 200); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should replace epoch if setTime is lesser than now and not tick', (t) => { |
| 100 | + t.mock.timers.enable(); |
| 101 | + const fn = t.mock.fn(); |
| 102 | + const id = setTimeout(fn, 1000); |
| 103 | + t.mock.timers.setTime(800); |
| 104 | + assert.strictEqual(Date.now(), 800); |
| 105 | + t.mock.timers.setTime(500); |
| 106 | + assert.strictEqual(Date.now(), 500); |
| 107 | + assert.strictEqual(fn.mock.callCount(), 0); |
| 108 | + clearTimeout(id); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should not tick time when setTime is called', (t) => { |
| 112 | + t.mock.timers.enable(); |
| 113 | + const fn = t.mock.fn(); |
| 114 | + const id = setTimeout(fn, 1000); |
| 115 | + t.mock.timers.setTime(1200); |
| 116 | + assert.strictEqual(Date.now(), 1200); |
| 117 | + assert.strictEqual(fn.mock.callCount(), 0); |
| 118 | + clearTimeout(id); |
| 119 | + }); |
| 120 | +}); |
0 commit comments