-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save Tracker instance on client.driver to support concurrent tests, b…
…ased on #41
- Loading branch information
Showing
6 changed files
with
85 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,41 +40,41 @@ export async function addUser(user: User): Promise<{ id }> { | |
import { expect } from '@jest/globals'; | ||
import { createTracker, MockClient } from 'knex-mock-client'; | ||
import { faker } from '@faker-js/faker'; | ||
import { db } from "../common/db-setup"; | ||
import { db } from '../common/db-setup'; | ||
|
||
jest.mock("../common/db-setup", () => { | ||
const knex = require("knex"); | ||
return { | ||
db: knex({ client: MockClient }), | ||
}; | ||
jest.mock('../common/db-setup', () => { | ||
const knex = require('knex'); | ||
return { | ||
db: knex({ client: MockClient }), | ||
}; | ||
}); | ||
|
||
describe('my-cool-controller tests', () => { | ||
let tracker: Tracker; | ||
let tracker: Tracker; | ||
|
||
beforeAll(() => { | ||
tracker = createTracker(db); | ||
}); | ||
|
||
beforeAll(() => { | ||
tracker = createTracker(db); | ||
}); | ||
afterEach(() => { | ||
tracker.reset(); | ||
}); | ||
|
||
afterEach(() => { | ||
tracker.reset(); | ||
}); | ||
it('should add new user', async () => { | ||
const insertId = faker.datatype.number(); | ||
tracker.on.insert('users').response([insertId]); | ||
|
||
it('should add new user', async () => { | ||
const insertId = faker.datatype.number(); | ||
tracker.on.insert('users').response([insertId]); | ||
|
||
const newUser = { name: 'foo bar', email: '[email protected]' }; | ||
const data = await addUser(newUser); | ||
const newUser = { name: 'foo bar', email: '[email protected]' }; | ||
const data = await addUser(newUser); | ||
|
||
expect(data.id).toEqual(insertId); | ||
expect(data.id).toEqual(insertId); | ||
|
||
const insertHistory = tracker.history.insert; | ||
const insertHistory = tracker.history.insert; | ||
|
||
expect(insertHistory).toHaveLength(1); | ||
expect(insertHistory[0].method).toEqual('insert'); | ||
expect(insertHistory[0].bindings).toEqual([newUser.name, newUser.email]); | ||
}); | ||
expect(insertHistory).toHaveLength(1); | ||
expect(insertHistory[0].method).toEqual('insert'); | ||
expect(insertHistory[0].bindings).toEqual([newUser.name, newUser.email]); | ||
}); | ||
}); | ||
``` | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { Knex } from 'knex'; | ||
import { Tracker, TrackerConfig } from './Tracker'; | ||
import { Tracker } from './Tracker'; | ||
|
||
export { MockClient } from './MockClient'; | ||
export { Tracker } from './Tracker'; | ||
export type { RawQuery, QueryMatcher, FunctionQueryMatcher } from '../types/mock-client'; | ||
|
||
export function createTracker(db: Knex, trackerConfig: TrackerConfig = {}): Tracker { | ||
const tracker = new Tracker(trackerConfig); | ||
db.client.config.tracker = tracker; | ||
export function createTracker(db: Knex): Tracker { | ||
const tracker = new Tracker(); | ||
|
||
db.client.setTracker(tracker); | ||
|
||
return tracker; | ||
} |
This file contains 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
This file contains 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