Closed hassankhan closed 5 years ago
Could this be a typescript issue with loading json files? https://hackernoon.com/import-json-into-typescript-8d465beded79 I have confirmed that package.json
is bundled in knex 13 and can't reproduce this error locally.
Thanks for the speedy reply 😄!
I've done as the link said, still no luck 😢 . I'm not quite sure where the problem could be. I was hoping someone else had already done this and might see the issue.
EDIT: It might be ts-jest
, which is used to transpile TypeScript before running in Jest, I'll update if I find anything else.
Could path mapping help? https://www.typescriptlang.org/docs/handbook/module-resolution.html
I found a way to get it to work (somewhat), I changed this line to the following:
import knexPackage from 'knex/package.json';
I'm going to look into why it thinks I'm using Knex v0.8 (I'm using v0.13). Also I had trouble building using the Makefile, I had to change this to BABEL = ./node_modules/babel-cli/bin/babel.js
(perhaps you're using Babel 5?)
After digging around in the source, it seems like perhaps this is causing this new issue. I'm using client.destroy()
as a promise, and mock-knex
seems to only be able to handle callbacks for the same functionality.
EDIT: Changing this line to the following allows me to finally run tests successfully (although now mock-knex
's tests seem to be failing 😖):
destroyRawConnection : (con, cb) => {
const promise = Promise.resolve();
if (cb && typeof cb === 'function') {
promise.then(cb.bind(null, null), cb);
}
return promise;
},
Any thoughts, @jbrumwell?
Hey @jbrumwell, have you had a chance to look over the issue? Would it be okay to make a PR with this functionality?
Hi, I experience this as well when running my tests with jest 20.0.4 (so, setup is similar as above described by @hassankhan but without typescript). It could get fixed when I change the line var _package = require('knex/package');
of the transpiled mock-knex code in my _./nodemodules/mock-knex/dist/index.js to var _package = require('knex/package.json');
. Of course this is not feasable for going on...so, any solving of this problem would be highly appreciated.
@hassankhan Can you create a failing test for me using client.destroy()
?
I will make the change requiring package.json in the next release sorry for the delay :)
Any movement on this? I see an open PR that appears to fix the problem for me.
With "ts-jest" I just added "json" in "moduleFileExtensions" and it works now
# ./jest.config.js
module.exports = {
globals: {
'ts-jest': {
tsConfigFile: 'tsconfig.json',
},
},
moduleFileExtensions: ['ts', 'js', 'json'],
transform: {
'^.+\\.(ts|tsx)$': './node_modules/ts-jest/preprocessor.js',
},
testMatch: ['**/*.test.ts'],
testEnvironment: 'node',
};
this project npm install
This should be resolved, https://github.com/colonyamerican/mock-knex/blob/master/src/index.js#L5
Hi there,
Thanks for the awesome package! Unfortunately I can't seem to get it to work. I'm using
jest@^20.0.0
,knex@^0.13.0
andmock-knex@^0.3.9
andtypescript@^2.3.3
:Jest test case
```js import * as Knex from "knex"; import * as MockKnex from "mock-knex"; import RedshiftAdapter from "../../src/adapters/RedshiftAdapter"; import BaseQuery from "../../src/queries/BaseQuery"; class MockQuery extends BaseQuery { constructor(params) { super(params); } public execute = (client) => { return client("logs").select("*").limit(5); } } describe("RedshiftAdapter", () => { let adapter: RedshiftAdapter; let client: MockKnex; let tracker; beforeAll(() => { client = Knex({ acquireConnectionTimeout: 2000, client: "pg", connection: { host : "host", password : "password", port : 1000, user : "user", database : "database", ssl : true, }, debug: true, }); MockKnex.mock(client); tracker = MockKnex.getTracker(); adapter = new RedshiftAdapter(client); }); beforeEach(() => { tracker.install(); }); afterAll(() => { MockKnex.unmock(client); }); afterEach(() => { tracker.uninstall(); }); it("can make a request to Redshift", () => { tracker.on("query", (query) => { expect(query.method).toEqual("select"); query.response([ { action : "get", resource : "Apps", }, { action : "get", resource : "Users", }, { action : "get", resource : "AppUsers", }, ]); }); return adapter.execute(new MockQuery({})) .then((result) => { console.log(result); }); }); }); ```