From 947601ec7ddf5fd0947e18bed48090a4aa2a2ef7 Mon Sep 17 00:00:00 2001 From: Shing Rui Date: Wed, 29 May 2024 21:59:03 +0800 Subject: [PATCH] Change coverageProvider. --- __test__/api/user.test.ts | 36 +++++++++++++++++++++++++++++++ __test__/components/Login.test.ts | 29 +++++++++++++++++++++++-- __test__/utils/date.test.ts | 7 ++++++ jest.config.js | 8 +++++-- sonar-project.properties | 2 +- src/components/User.vue | 2 +- src/components/layout/Header.vue | 2 +- src/router/index.ts | 1 - 8 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 __test__/api/user.test.ts create mode 100644 __test__/utils/date.test.ts diff --git a/__test__/api/user.test.ts b/__test__/api/user.test.ts new file mode 100644 index 0000000..8b40506 --- /dev/null +++ b/__test__/api/user.test.ts @@ -0,0 +1,36 @@ +import sqlite3, { Database } from 'sqlite3' +import sqlite3Rewire, { + options, + execute, + save, + list, + page, + count, + getOne, + updateById, + remove +} from '../../src/db/sqlite3' + +jest.mock('sqlite3') + +beforeEach(() => { + jest.clearAllMocks() +}) + +describe('Test user api', () => { + test('Test pageList', async () => { + const getDb = sqlite3Rewire.__get__('getDb') + await getDb() + expect(options.db).toBeInstanceOf(Database) + + sqlite3.Database = jest.fn().mockImplementation((filePath, callback) => { + if (callback !== undefined) { + callback(new Error('Test')) + } + }) + + options.db = undefined + await getDb() + expect(options.db).toBeInstanceOf(sqlite3.Database) + }) +}) diff --git a/__test__/components/Login.test.ts b/__test__/components/Login.test.ts index 6b2d11a..4d66e6d 100644 --- a/__test__/components/Login.test.ts +++ b/__test__/components/Login.test.ts @@ -1,9 +1,12 @@ +import sqlite3 from 'sqlite3' import { mount } from '@vue/test-utils' import Login from '../../src/components/Login.vue' import Antd from 'ant-design-vue' import store from '../../src/store' import i18n from '../../src/i18n' +jest.mock('sqlite3') + Object.defineProperty(window, 'matchMedia', { writable: true, value: jest.fn().mockImplementation((query) => ({ @@ -18,12 +21,34 @@ Object.defineProperty(window, 'matchMedia', { })) }) -test('Test Login.vue', async () => { +describe('Test Login.vue', () => { const wrapper = mount(Login, { propsData: {}, global: { plugins: [Antd, store, i18n] } }) - expect(wrapper.exists()).toBe(true) + + test('Mount component', async () => { + expect(wrapper.exists()).toBe(true) + }) + + test('Submit the form', async () => { + // Mock database instance + sqlite3.Database = jest.fn().mockImplementation(() => { + return { + get: jest.fn(function (sql, callback) { + callback(undefined, { id: 1, content: 'Hello World!', count: 1 }) + }) + } + }) + + // Input username and password. + const usernameInput = wrapper.find('input[id="normal_login_username"]') + const passwordInput = wrapper.find('input[id="normal_login_password"]') + await usernameInput.setValue('admin') + await passwordInput.setValue('admin') + + await wrapper.find('form').trigger('submit') + }) }) diff --git a/__test__/utils/date.test.ts b/__test__/utils/date.test.ts new file mode 100644 index 0000000..f43e483 --- /dev/null +++ b/__test__/utils/date.test.ts @@ -0,0 +1,7 @@ +import { datetime } from '../../src/utils/date' +describe('Test date utils', () => { + test('Test datetime', async () => { + const date = new Date('2024-05-29T00:00:00Z') + expect(datetime(date)).toBe('2024-05-29 00:00:00') + }) +}) diff --git a/jest.config.js b/jest.config.js index a1564e3..1d58e17 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,16 +1,20 @@ module.exports = { transform: { - "\\.[jt]sx?$": "babel-jest", + '\\.[jt]sx?$': 'babel-jest', '^[^.]+.vue$': '@vue/vue3-jest' }, testPathIgnorePatterns: ['/node_modules/'], collectCoverage: true, collectCoverageFrom: ['src/**/**.vue', 'src/**/**.js', 'src/**/**.ts'], coverageDirectory: 'coverage', + coverageProvider: 'v8', coverageReporters: ['text', 'lcov'], moduleFileExtensions: ['vue', 'js', 'ts'], testEnvironment: 'jsdom', testEnvironmentOptions: { customExportConditions: ['node', 'node-addons'] - } + }, + // moduleNameMapper: { + // '\\.(css|less)$': '/__test__/styleMock.ts' + // } } diff --git a/sonar-project.properties b/sonar-project.properties index f6f0aa0..fa79a57 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,5 +1,5 @@ sonar.organization=sunquakes sonar.projectKey=sunquakes_electron-kits -sonar.exclusions=src/lang/**.js +sonar.exclusions=src/lang/**.ts sonar.javascript.lcov.reportPaths=coverage/lcov.info \ No newline at end of file diff --git a/src/components/User.vue b/src/components/User.vue index a3e5dcb..fb9c434 100644 --- a/src/components/User.vue +++ b/src/components/User.vue @@ -63,7 +63,7 @@