This file contains hidden or 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
import { JSDOM } from "jsdom"; | |
import { init } from "../../src/index"; | |
import axios from "axios"; | |
const config = { | |
url: "https://www.example.com/", | |
domain: "example.com", | |
}; | |
const dom = new JSDOM("", config); | |
global.document = dom.window.document; |
This file contains hidden or 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
import { counter } from "./demo"; | |
import { request } from "./demo/wrap-request"; | |
jest.mock("./demo/wrap-request"); | |
describe("Simple mock", () => { | |
it("test success", () => { | |
request.mockImplementation(() => Promise.resolve({ result: 0 })); | |
return counter(1, 2).then(res => { | |
expect(res).toStrictEqual({ result: 0, msg: "success" }); |
This file contains hidden or 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
import { counter } from "./demo"; | |
import * as request from "./demo/wrap-request"; | |
jest.mock("./demo/wrap-request", () => { | |
let hook = () => ({ result: 0 }); | |
return { | |
setHook: cb => (hook = cb), | |
request: (...args) => { | |
return new Promise(resolve => { | |
resolve(hook(...args)); |
This file contains hidden or 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
import { counter } from "./demo"; | |
import { request } from "./demo/wrap-request"; | |
jest.mock("./demo/wrap-request"); | |
describe("Simple mock", () => { | |
it("test success", () => { | |
request.mockResolvedValue({ result: 0 }); | |
return counter(1, 2).then(res => { | |
expect(res).toStrictEqual({ result: 0, msg: "success" }); |
This file contains hidden or 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
import { JSDOM } from "jsdom"; | |
const config = { | |
url: "https://www.example.com/", | |
domain: "example.com", | |
}; | |
const dom = new JSDOM("", config); | |
global.document = dom.window.document; | |
global.document.domain = config.domain; | |
global.window = dom.window; |
This file contains hidden or 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
import { request } from "./wrap-request"; | |
export const counter = (id: number, number: number): Promise<{ result: number; msg: string }> => { | |
const operate = number > 0 ? 1 : -1; | |
return request({ | |
url: "https://www.example.com/api/setCounter", | |
method: "POST", | |
data: { id, operate }, | |
}) | |
.then(res => { |
This file contains hidden or 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
import axios, { AxiosRequestConfig } from "axios"; | |
const instance = axios.create({ | |
timeout: 3000, | |
}); | |
export const request = (options: AxiosRequestConfig): Promise<any> => { | |
// do something wrap | |
return instance.request(options).then(res => res.data); | |
}; |
This file contains hidden or 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
module.exports = { | |
roots: ['<rootDir>/src'], // Find files in the src/root directory | |
collectCoverage: true, // Statistical coverage | |
coverageDirectory: 'coverage', // The folder where coverage results are output | |
coverageThreshold: { | |
// Total coverage requirements for all documents | |
global: { | |
branches: 80, | |
functions: 80, | |
lines: 80, |
This file contains hidden or 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
{ | |
"scripts": { | |
"test:staged": "jest --bail --findRelatedTests" | |
}, | |
"lint-staged": { | |
"src/**/*.{js,jsx,ts,tsx}": ["npm run test:staged"] | |
} | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>React</title> | |
</head> | |
<body> | |
<div id="root"></div> |