Browse Source

chore: fix jest env

Gerald 3 years ago
parent
commit
a2edec49e1
9 changed files with 382 additions and 450 deletions
  1. 6 5
      package.json
  2. 10 13
      test/common/index.test.js
  3. 0 1
      test/mock/base.js
  4. 11 0
      test/mock/env.js
  5. 0 3
      test/mock/index.js
  6. 0 2
      test/mock/polyfill.js
  7. 0 2
      test/mock/register.js
  8. 0 37
      test/mock/time.js
  9. 355 387
      yarn.lock

+ 6 - 5
package.json

@@ -28,7 +28,7 @@
     "@babel/plugin-syntax-function-bind": "^7.18.6",
     "@gera2ld/plaid": "~2.5.6",
     "@gera2ld/plaid-common-ts": "~2.5.1",
-    "@gera2ld/plaid-test": "~2.5.7",
+    "@gera2ld/plaid-test": "~2.5.8",
     "@gera2ld/plaid-webpack": "~2.5.7",
     "@gera2ld/plaid-webpack-vue3": "~2.5.7",
     "@types/chrome": "^0.0.191",
@@ -41,8 +41,8 @@
     "gulp": "^4.0.2",
     "gulp-plumber": "^1.1.0",
     "husky": "^8.0.1",
+    "jest-environment-jsdom": "^29.0.3",
     "js-yaml": "^4.1.0",
-    "jsdom": "^20.0.0",
     "plugin-error": "^2.0.0",
     "postcss-scss": "^4.0.4",
     "postcss-simple-vars": "^6.0.3",
@@ -74,9 +74,10 @@
     "node": ">=12"
   },
   "jest": {
-    "setupFiles": [
+    "setupFilesAfterEnv": [
       "./test/mock/index.js"
-    ]
+    ],
+    "testEnvironment": "./test/mock/env.js"
   },
   "beta": 2
-}
+}

+ 10 - 13
test/common/index.test.js

@@ -1,7 +1,8 @@
 import {
   isRemote, compareVersion, debounce, throttle,
 } from '@/common';
-import { mocker } from '../mock';
+
+jest.useFakeTimers();
 
 test('isRemote', () => {
   [
@@ -94,14 +95,12 @@ test('debounce', () => {
     log.push(i);
   }, 500);
   for (let i = 0; i < 3; i += 1) {
-    fn(i);
-    mocker.clock.tick(200);
+    setTimeout(fn, 200 * i, i);
   }
-  mocker.clock.tick(500);
   for (let i = 0; i < 3; i += 1) {
-    fn(i);
-    mocker.clock.tick(600);
+    setTimeout(fn, 1200 + 600 * i, i);
   }
+  jest.runAllTimers();
   expect(log).toEqual([2, 0, 1, 2]);
 });
 
@@ -114,7 +113,7 @@ test('debounce with invalid time', () => {
     for (let i = 0; i < 3; i += 1) {
       fn(i);
     }
-    mocker.clock.tick(500);
+    jest.runAllTimers();
     expect(log).toEqual([2]);
   }
 });
@@ -125,14 +124,12 @@ test('throttle', () => {
     log.push(i);
   }, 500);
   for (let i = 0; i < 6; i += 1) {
-    fn(i);
-    mocker.clock.tick(200);
+    setTimeout(fn, 200 * i, i);
   }
-  mocker.clock.tick(500);
   for (let i = 0; i < 3; i += 1) {
-    fn(i);
-    mocker.clock.tick(600);
+    setTimeout(fn, 1200 + 600 * i, i);
   }
+  jest.runAllTimers();
   expect(log).toEqual([0, 3, 0, 1, 2]);
 });
 
@@ -145,7 +142,7 @@ test('throttle with invalid time', () => {
     for (let i = 0; i < 3; i += 1) {
       fn(i);
     }
-    mocker.clock.tick(500);
+    jest.runAllTimers();
     expect(log).toEqual([0]);
   }
 });

+ 0 - 1
test/mock/base.js

@@ -1 +0,0 @@
-export const mocker = {};

+ 11 - 0
test/mock/env.js

@@ -0,0 +1,11 @@
+const Environment = require('jest-environment-jsdom').default;
+
+module.exports = class CustomTestEnvironment extends Environment {
+  async setup() {
+    await super.setup();
+    this.global.MessageChannel ||= MessageChannel;
+    this.global.MessagePort ||= MessagePort;
+    this.global.TextEncoder ||= TextEncoder;
+    this.global.TextDecoder ||= TextDecoder;
+  }
+};

+ 0 - 3
test/mock/index.js

@@ -1,4 +1 @@
 import './polyfill';
-import './time';
-
-export * from './base';

+ 0 - 2
test/mock/polyfill.js

@@ -1,7 +1,5 @@
 import tldRules from 'tldjs/rules.json';
-import { JSDOM } from 'jsdom';
 
-global.window = new JSDOM('').window;
 global.browser = {
   storage: {
     local: {

+ 0 - 2
test/mock/register.js

@@ -1,2 +0,0 @@
-require('@babel/register');
-require('./index');

+ 0 - 37
test/mock/time.js

@@ -1,37 +0,0 @@
-import { mocker } from './base';
-
-let time = 0;
-let timers = [];
-
-function tick(delta) {
-  time += delta;
-  let scheduledTimers = [];
-  const filterTimer = ({ fn, ts, args }) => {
-    if (time >= ts) {
-      fn(...args);
-      return false;
-    }
-    return true;
-  };
-  while (timers.length) {
-    const processTimers = timers;
-    timers = [];
-    scheduledTimers = scheduledTimers.concat(processTimers.filter(filterTimer));
-  }
-  timers = scheduledTimers;
-}
-
-function now() {
-  return time;
-}
-
-function setTimeout(fn, delta, ...args) {
-  timers.push({ fn, ts: time + delta, args });
-  return timers.length;
-}
-
-global.performance = { now };
-global.Date.now = now;
-global.setTimeout = setTimeout;
-
-mocker.clock = { tick };

File diff suppressed because it is too large
+ 355 - 387
yarn.lock


Some files were not shown because too many files changed in this diff