Reputation: 6013
I'm trying to use SuperTest to test an Apollo Server following the first answer to this Stack Overflow question, among other examples I've found.
My code in its entirety is
// /__test__/index.test.ts
import * as request from 'supertest';
let postData = {
query: `query allArticles{
allArticles{
id
}
}`,
operationName: 'allArticles'
};
test('basic', async () => {
try {
const response = request
.post('/graphql')
.send(postData)
.expect(200); // status code that you expect to be returned
console.log('response', response);
} catch (error) {
console.log(`error ${error.toString()}`);
}
});
However when I run this with Jest
"test": "jest --detectOpenHandles --colors"
I get
PASS __test__/index.test.ts
● Console
console.log
error TypeError: request.post is not a function
at __test__/index.test.ts:20:11
For what it's worth, I don't think it's "passing" the test, as it doesn't matter what I put in the expect
.
If I change my code to follow the Stack Overflow exactly (passing the GraphQL endpoint directly to request
test('basic', async () => {
try {
const response = request('/graphql')
.post('/graphql')
.send(postData)
.expect(200); // status code that you expect to be returned
console.log('response', response);
} catch (error) {
console.log(`error ${error.toString()}`);
}
});
I get
PASS __test__/index.test.ts
● Console
console.log
error TypeError: request is not a function
at __test__/index.test.ts:20:11
I'm using ts-jest
, and running under Node 12.14
My tsconfig.json
is
{
"compilerOptions": {
"target": "ES6",
"lib": [
"esnext",
"dom"
],
"skipLibCheck": true,
"outDir": "dist",
"strict": false,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"sourceMap": true,
"alwaysStrict": true
},
"exclude": [
"node_modules",
"**/*.test.ts",
"**/*.mock.ts"
]
}
and my jest.config
is
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node'
};
Any clues appreciated!
Upvotes: 1
Views: 4387
Reputation: 24555
supertest
has no export, which is why need to change your import to
import {default as request} from 'supertest';
request
is now the exported factory function which you can invoke:
const response = request('/graphql')
.post('/graphql')
...
Upvotes: 3