fix unit test failure

This commit is contained in:
rick 2024-07-31 10:25:06 +08:00
parent cbafc3d212
commit c7c435a9ee
6 changed files with 127 additions and 105 deletions

View File

@ -41,6 +41,7 @@
"eslint-plugin-vue": "^9.11.0", "eslint-plugin-vue": "^9.11.0",
"fetch-mock-jest": "^1.5.1", "fetch-mock-jest": "^1.5.1",
"jest": "^29.6.1", "jest": "^29.6.1",
"jest-fetch-mock": "^3.0.3",
"jsdom": "^22.1.0", "jsdom": "^22.1.0",
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"prettier": "^2.8.8", "prettier": "^2.8.8",
@ -4282,6 +4283,15 @@
"integrity": "sha512-VQ2MBenTq1fWZUH9DJNGti7kKv6EeAuYr3cLwxUWhIu1baTaXh4Ib5W2CqHVqib4/MqbYGJqiL3Zb8GJZr3l4g==", "integrity": "sha512-VQ2MBenTq1fWZUH9DJNGti7kKv6EeAuYr3cLwxUWhIu1baTaXh4Ib5W2CqHVqib4/MqbYGJqiL3Zb8GJZr3l4g==",
"peer": true "peer": true
}, },
"node_modules/cross-fetch": {
"version": "3.1.8",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz",
"integrity": "sha512-cvA+JwZoU0Xq+h6WkMvAUqPEYy92Obet6UdKLfW60qn99ftItKjB5T+BkyWOFWe2pUyfQ+IJHmpOTznqk1M6Kg==",
"dev": true,
"dependencies": {
"node-fetch": "^2.6.12"
}
},
"node_modules/cross-spawn": { "node_modules/cross-spawn": {
"version": "7.0.3", "version": "7.0.3",
"resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.3.tgz", "resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.3.tgz",
@ -7499,6 +7509,16 @@
"node": "^14.15.0 || ^16.10.0 || >=18.0.0" "node": "^14.15.0 || ^16.10.0 || >=18.0.0"
} }
}, },
"node_modules/jest-fetch-mock": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/jest-fetch-mock/-/jest-fetch-mock-3.0.3.tgz",
"integrity": "sha512-Ux1nWprtLrdrH4XwE7O7InRY6psIi3GOsqNESJgMJ+M5cv4A8Lh7SN9d2V2kKRZ8ebAfcd1LNyZguAOb6JiDqw==",
"dev": true,
"dependencies": {
"cross-fetch": "^3.0.4",
"promise-polyfill": "^8.1.3"
}
},
"node_modules/jest-get-type": { "node_modules/jest-get-type": {
"version": "29.4.3", "version": "29.4.3",
"resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-29.4.3.tgz", "resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-29.4.3.tgz",
@ -9306,8 +9326,6 @@
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.12.tgz", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.12.tgz",
"integrity": "sha512-C/fGU2E8ToujUivIO0H+tpQ6HWo4eEmchoPIoXtxCrVghxdKq+QOHqEZW7tuP3KlV3bC8FRMO5nMCC7Zm1VP6g==", "integrity": "sha512-C/fGU2E8ToujUivIO0H+tpQ6HWo4eEmchoPIoXtxCrVghxdKq+QOHqEZW7tuP3KlV3bC8FRMO5nMCC7Zm1VP6g==",
"dev": true, "dev": true,
"optional": true,
"peer": true,
"dependencies": { "dependencies": {
"whatwg-url": "^5.0.0" "whatwg-url": "^5.0.0"
}, },
@ -9327,25 +9345,19 @@
"version": "0.0.3", "version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
"dev": true, "dev": true
"optional": true,
"peer": true
}, },
"node_modules/node-fetch/node_modules/webidl-conversions": { "node_modules/node-fetch/node_modules/webidl-conversions": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
"dev": true, "dev": true
"optional": true,
"peer": true
}, },
"node_modules/node-fetch/node_modules/whatwg-url": { "node_modules/node-fetch/node_modules/whatwg-url": {
"version": "5.0.0", "version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"dev": true, "dev": true,
"optional": true,
"peer": true,
"dependencies": { "dependencies": {
"tr46": "~0.0.3", "tr46": "~0.0.3",
"webidl-conversions": "^3.0.0" "webidl-conversions": "^3.0.0"
@ -10032,6 +10044,12 @@
"asap": "~2.0.6" "asap": "~2.0.6"
} }
}, },
"node_modules/promise-polyfill": {
"version": "8.3.0",
"resolved": "https://registry.npmjs.org/promise-polyfill/-/promise-polyfill-8.3.0.tgz",
"integrity": "sha512-H5oELycFml5yto/atYqmjyigJoAo3+OXwolYiH7OfQuYlAqhxNvTfiNMbV9hsC6Yp83yE5r2KTVmtrG6R9i6Pg==",
"dev": true
},
"node_modules/prompts": { "node_modules/prompts": {
"version": "2.4.2", "version": "2.4.2",
"resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz", "resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz",
@ -15369,6 +15387,15 @@
"integrity": "sha512-VQ2MBenTq1fWZUH9DJNGti7kKv6EeAuYr3cLwxUWhIu1baTaXh4Ib5W2CqHVqib4/MqbYGJqiL3Zb8GJZr3l4g==", "integrity": "sha512-VQ2MBenTq1fWZUH9DJNGti7kKv6EeAuYr3cLwxUWhIu1baTaXh4Ib5W2CqHVqib4/MqbYGJqiL3Zb8GJZr3l4g==",
"peer": true "peer": true
}, },
"cross-fetch": {
"version": "3.1.8",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.8.tgz",
"integrity": "sha512-cvA+JwZoU0Xq+h6WkMvAUqPEYy92Obet6UdKLfW60qn99ftItKjB5T+BkyWOFWe2pUyfQ+IJHmpOTznqk1M6Kg==",
"dev": true,
"requires": {
"node-fetch": "^2.6.12"
}
},
"cross-spawn": { "cross-spawn": {
"version": "7.0.3", "version": "7.0.3",
"resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.3.tgz", "resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.3.tgz",
@ -17860,6 +17887,16 @@
"jest-util": "^29.6.1" "jest-util": "^29.6.1"
} }
}, },
"jest-fetch-mock": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/jest-fetch-mock/-/jest-fetch-mock-3.0.3.tgz",
"integrity": "sha512-Ux1nWprtLrdrH4XwE7O7InRY6psIi3GOsqNESJgMJ+M5cv4A8Lh7SN9d2V2kKRZ8ebAfcd1LNyZguAOb6JiDqw==",
"dev": true,
"requires": {
"cross-fetch": "^3.0.4",
"promise-polyfill": "^8.1.3"
}
},
"jest-get-type": { "jest-get-type": {
"version": "29.4.3", "version": "29.4.3",
"resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-29.4.3.tgz", "resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-29.4.3.tgz",
@ -19260,8 +19297,6 @@
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.12.tgz", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.12.tgz",
"integrity": "sha512-C/fGU2E8ToujUivIO0H+tpQ6HWo4eEmchoPIoXtxCrVghxdKq+QOHqEZW7tuP3KlV3bC8FRMO5nMCC7Zm1VP6g==", "integrity": "sha512-C/fGU2E8ToujUivIO0H+tpQ6HWo4eEmchoPIoXtxCrVghxdKq+QOHqEZW7tuP3KlV3bC8FRMO5nMCC7Zm1VP6g==",
"dev": true, "dev": true,
"optional": true,
"peer": true,
"requires": { "requires": {
"whatwg-url": "^5.0.0" "whatwg-url": "^5.0.0"
}, },
@ -19270,25 +19305,19 @@
"version": "0.0.3", "version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
"dev": true, "dev": true
"optional": true,
"peer": true
}, },
"webidl-conversions": { "webidl-conversions": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
"dev": true, "dev": true
"optional": true,
"peer": true
}, },
"whatwg-url": { "whatwg-url": {
"version": "5.0.0", "version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"dev": true, "dev": true,
"optional": true,
"peer": true,
"requires": { "requires": {
"tr46": "~0.0.3", "tr46": "~0.0.3",
"webidl-conversions": "^3.0.0" "webidl-conversions": "^3.0.0"
@ -19819,6 +19848,12 @@
"asap": "~2.0.6" "asap": "~2.0.6"
} }
}, },
"promise-polyfill": {
"version": "8.3.0",
"resolved": "https://registry.npmjs.org/promise-polyfill/-/promise-polyfill-8.3.0.tgz",
"integrity": "sha512-H5oELycFml5yto/atYqmjyigJoAo3+OXwolYiH7OfQuYlAqhxNvTfiNMbV9hsC6Yp83yE5r2KTVmtrG6R9i6Pg==",
"dev": true
},
"prompts": { "prompts": {
"version": "2.4.2", "version": "2.4.2",
"resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz", "resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.2.tgz",

View File

@ -49,6 +49,7 @@
"eslint-plugin-vue": "^9.11.0", "eslint-plugin-vue": "^9.11.0",
"fetch-mock-jest": "^1.5.1", "fetch-mock-jest": "^1.5.1",
"jest": "^29.6.1", "jest": "^29.6.1",
"jest-fetch-mock": "^3.0.3",
"jsdom": "^22.1.0", "jsdom": "^22.1.0",
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"prettier": "^2.8.8", "prettier": "^2.8.8",

View File

@ -16,37 +16,9 @@ limitations under the License.
import { describe } from 'node:test' import { describe } from 'node:test'
import {Cache, SetPreference, TestCaseResponse, Store, Stores} from '../cache' import {Cache, SetPreference, TestCaseResponse, Store, Stores} from '../cache'
import { SetupStorage } from './common'
const localStorageMock = (() => { SetupStorage()
let store = {};
return {
getItem(key) {
return store[key] || null;
},
setItem(key, value) {
store[key] = value.toString();
},
removeItem(key) {
delete store[key];
},
clear() {
store = {};
}
};
})();
Object.defineProperty(global, 'sessionStorage', {
value: localStorageMock
});
Object.defineProperty(global, 'localStorage', {
value: localStorageMock
});
Object.defineProperty(global, 'navigator', {
value: {
language: 'en'
}
});
describe('TestCaseResponseCache', () => { describe('TestCaseResponseCache', () => {
test('should set and get test case response cache', () => { test('should set and get test case response cache', () => {

View File

@ -0,0 +1,48 @@
/*
Copyright 2024 API Testing Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
export function SetupStorage() {
const localStorageMock = (() => {
let store = new Map<string, string>;
return {
getItem(key: string) {
return store.get(key) || null;
},
setItem(key: string, value: string) {
store.set(key, value);
},
removeItem(key: string) {
store.delete(key)
},
clear() {
store.clear();
}
};
})();
Object.defineProperty(global, 'sessionStorage', {
value: localStorageMock
});
Object.defineProperty(global, 'localStorage', {
value: localStorageMock
});
Object.defineProperty(global, 'navigator', {
value: {
language: 'en'
}
});
}

View File

@ -15,52 +15,28 @@ limitations under the License.
*/ */
import { describe } from 'node:test' import { describe } from 'node:test'
import {API, TestCase} from '../net' import {API} from '../net'
import { type TestCase } from '../net'
import { SetupStorage } from './common'
import fetchMock from "jest-fetch-mock";
const fakeFetch: { [key:string]:string; } = {}; fetchMock.enableMocks();
SetupStorage()
global.fetch = jest.fn((key: string) => beforeEach(() => {
Promise.resolve({ fetchMock.resetMocks();
json: () => {
if (fakeFetch[key] === undefined) {
return Promise.resolve({})
}
return Promise.resolve(JSON.parse(fakeFetch[key]))
},
}),
) as jest.Mock;
fakeFetch['/api/v1/version'] = '{"version":"0.0.1"}'
const localStorageMock = (() => {
let store = {};
return {
getItem(key) {
return store[key] || null;
},
setItem(key, value) {
store[key] = value.toString();
},
removeItem(key) {
delete store[key];
},
clear() {
store = {};
}
};
})();
Object.defineProperty(global, 'sessionStorage', {
value: localStorageMock
}); });
describe('net', () => { describe('net', () => {
test('GetVersion', () => { test('GetVersion', () => {
API.GetVersion() fetchMock.mockResponseOnce(`{"version":"v0.0.1"}`)
API.GetVersion((d) => {
expect(d.version).toEqual('v0.0.2')
})
}) })
test('CreateTestSuite', () => { test('CreateTestSuite', () => {
fetchMock.mockResponseOnce(`{"version":"v0.0.1"}`)
API.CreateTestSuite({ API.CreateTestSuite({
store: 'store', store: 'store',
name: 'name', name: 'name',
@ -72,27 +48,27 @@ describe('net', () => {
}) })
test('UpdateTestSuite', () => { test('UpdateTestSuite', () => {
API.UpdateTestSuite({}, (d) => {}) API.UpdateTestSuite({}, () => {})
}) })
test('GetTestSuite', () => { test('GetTestSuite', () => {
API.GetTestSuite('fake', (d) => {}) API.GetTestSuite('fake', () => {})
}) })
test('DeleteTestSuite', () => { test('DeleteTestSuite', () => {
API.DeleteTestSuite('fake', (d) => {}) API.DeleteTestSuite('fake', () => {})
}) })
test('ConvertTestSuite', () => { test('ConvertTestSuite', () => {
API.ConvertTestSuite('fake', 'generator', (d) => {}) API.ConvertTestSuite('fake', 'generator', () => {})
}) })
test('DuplicateTestSuite', () => { test('DuplicateTestSuite', () => {
API.DuplicateTestSuite('source', 'target', (d) => {}) API.DuplicateTestSuite('source', 'target', () => {})
}) })
test('GetTestSuiteYaml', () => { test('GetTestSuiteYaml', () => {
API.GetTestSuiteYaml('fake', (d) => {}) API.GetTestSuiteYaml('fake', () => {})
}) })
test('CreateTestCase', () => { test('CreateTestCase', () => {

View File

@ -17,19 +17,9 @@ limitations under the License.
import { describe } from 'node:test' import { describe } from 'node:test'
import { NewSuggestedAPIsQuery, CreateFilter, GetHTTPMethods, FlattenObject } from '../types' import { NewSuggestedAPIsQuery, CreateFilter, GetHTTPMethods, FlattenObject } from '../types'
import type { Pair } from '../types' import type { Pair } from '../types'
import fetchMock from "jest-fetch-mock";
const fakeFetch: { [key:string]:string; } = {}; fetchMock.enableMocks();
global.fetch = jest.fn((key: string) =>
Promise.resolve({
json: () => {
if (fakeFetch[key] === undefined) {
return Promise.resolve({})
}
return Promise.resolve(JSON.parse(fakeFetch[key]))
},
}),
) as jest.Mock;
describe('NewSuggestedAPIsQuery', () => { describe('NewSuggestedAPIsQuery', () => {
test('empty data', () => { test('empty data', () => {