I'm creating my API test framework with TypeScript and I'm new to it.
I created this helper file assertions.ts
import * as chai from 'chai';
interface response {
status: number
body: object
}
const assertResponseStatusCode =
(response:response, statusCode:number) =>
chai.expect(response.status).to.deep.equals(statusCode);
const assertSuccessResponseStatusCode = (response:response) =>
assertResponseStatusCode(response, 201)
export = {assertSuccessResponseStatusCode}
Then I started using them on my spec file as bellow
import * as auth from '../Helpers/auth';
import * as assert from '../Helpers/assertions';
import { user } from '../Samples/user';
describe('login', () => {
it('should return access token with valid credentials', async() => {
const response = await auth.login(user);
console.log(response.body);
assert.assertSuccessResponseStatusCode(response);
});
});
I'm getting this error This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.. for this line import * as assert from '../Helpers/assertions';
although esModuleInterop
is set as true in tsconfig.json
.
I just ran into this too and the other answer gives one solution but I think the better solution in this case is to change:
export = {assertSuccessResponseStatusCode}
To:
export {assertSuccessResponseStatusCode}
The first method is TypeScript's interop with CommonJS modules, but you can only import those like import assertions from './assertions'
. The second is an ESM export and can imported as either import { assertSuccessResponseStatusCode } from './assertions'
or import * as assertions from './assertions'
. It also allows you to have default exports along with the other exports.