Skip to content

Commit

Permalink
test: create dedicated sqlite db for each test suite (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi authored Jul 27, 2024
1 parent 2d51c3c commit 37c75d4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"build:watch": "npm run build -- --watch",
"commit": "npx git-cz",
"test": "jest --config ./test/jest.config.js",
"test:coverage": "cross-env NODE_ENV=test jest --config ./test/jest.config.js --coverage --runInBand",
"test:coverage": "cross-env NODE_ENV=test jest --config ./test/jest.config.js --coverage",
"lint": "eslint --ext .js,.vue,.ts ./src ./test",
"lint:fix": "npm run lint -- --fix",
"docs:dev": "vitepress dev docs --temp .temp",
Expand Down
6 changes: 3 additions & 3 deletions test/data/typeorm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
} from '../../../src';
import { createDataSourceOptions } from './factory';

export async function setupTestDatabase() : Promise<DataSource> {
export async function setupFsDataSource(name: string) : Promise<DataSource> {
const options = createDataSourceOptions();
Object.assign(options, {
database: 'test/data/typeorm/db.sqlite',
database: `writable/${name}.sqlite`,
});

await createDatabase({
Expand All @@ -25,7 +25,7 @@ export async function setupTestDatabase() : Promise<DataSource> {
return dataSource;
}

export async function destroyTestDatabase(dataSource: DataSource) {
export async function destroyTestFsDataSource(dataSource: DataSource) {
await dataSource.destroy();

const { options } = dataSource;
Expand Down
6 changes: 3 additions & 3 deletions test/unit/seeder/factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { DataSource } from 'typeorm';
import { useSeederFactory } from '../../../src';
import { User } from '../../data/entity/user';
import { destroyTestDatabase, setupTestDatabase } from '../../data/typeorm/utils';
import { destroyTestFsDataSource, setupFsDataSource } from '../../data/typeorm/utils';
import '../../data/factory/user';

describe('src/seeder/factory/index.ts', () => {
let dataSource : DataSource;
beforeEach(async () => {
dataSource = await setupTestDatabase();
dataSource = await setupFsDataSource('factory');
});

afterEach(async () => {
await destroyTestDatabase(dataSource);
await destroyTestFsDataSource(dataSource);
});

it('should create & save seed', async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/seeder/seeder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import {
} from '../../../src';
import type { SeederEntity } from '../../../src';
import { User } from '../../data/entity/user';
import { destroyTestDatabase, setupTestDatabase } from '../../data/typeorm/utils';
import { destroyTestFsDataSource, setupFsDataSource } from '../../data/typeorm/utils';
import '../../data/factory/user';
import UserSeeder from '../../data/seed/user';

describe('src/seeder/index.ts', () => {
let dataSource : DataSource;

beforeEach(async () => {
dataSource = await setupTestDatabase();
dataSource = await setupFsDataSource('seeder');
});

afterEach(async () => {
await destroyTestDatabase(dataSource);
await destroyTestFsDataSource(dataSource);
});

it('should seed with data-source options', async () => {
Expand Down
3 changes: 3 additions & 0 deletions writable/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
*/
!.gitignore

0 comments on commit 37c75d4

Please sign in to comment.