Having trouble mocking an import in my spec file and seeking help to identify the issue.
This is the exported class for the Database connection:
import Knex from 'knex';
import { merge } from 'lodash';
import knexfile from '../knexfile';
class Database {
private knexInstance: Knex;
private config: object;
connect(options = {}): void {
if (this.knexInstance) {
return;
}
this.config = merge({}, knexfile, options);
this.knexInstance = Knex(this.config);
}
get query(): Knex {
if (!this.knexInstance) {
this.connect();
}
return this.knexInstance;
}
close(done): void {
if (!this.knexInstance) {
done();
return;
}
this.knexInstance.destroy(done);
}
}
export default new Database();
This is the action file attempting to use the database file:
import db from '../../database';
const tableName = 'attempts';
export const typeDef = `
extend type Query {
attempt(id: String): Attempt!
}
extend type Mutation {
createAttempt(questionId: String!, attemptId: String!, choiceId: String): Attempt
}
type Attempt {
id: String!
correctanswers: Int!
userid: String!
examid: String!
}
`;
export const resolvers = {
Query: {
attempt(_, { id = '' }) {
return db
.query(tableName)
.where({ id })
.first();
},
},
Mutation: {
async createAttempt(root, args) {
const [answer] = await db
.query(tableName)
.insert(args)
.returning('*');
return answer;
},
},
};
And here is the test file:
import { createSandbox } from 'sinon';
import { resolvers } from './answer';
import db from '../../database';
import * as should from 'should';
const sandbox = createSandbox();
describe('Answer', () => {
afterEach(() => sandbox.restore());
describe('Query Answer', () => {
it('should return answer by id', async () => {
const expected = { id: 'xxx' };
const firstSpy = sandbox.fake.resolves(expected);
const whereSpy = sandbox.fake.resolves({
first: firstSpy,
});
// The stub doesn't seem to be called. Import not replaced with stub in implementation file.
const querySpy = sandbox.stub(db, 'query').callsFake(() => {
return Promise.resolve({
where: whereSpy,
});
});
const inputId = '100';
const result = await resolvers.Query.answer(null, { id: inputId });
sandbox.assert.calledOnce(querySpy);
sandbox.assert.calledOnce(whereSpy);
sandbox.assert.calledOnce(firstSpy);
result.should.deepEqual(expected);
});
});
});
Facing issues where the import is not being replaced with the stub in the implementation file during tests.