Everything is ok when my ts-node version is 8.x ~ 9.x , but sinon stub failed when I upgraded ts-node to 10.x
The code likes below:
// file: ./lambda.ts
export const getAWSLambda = () => new AWS.Lambda({ region: AWS_DEFAULT_REGION });
export class AWSLambda {
private readonly lambda: AWS.Lambda;
constructor() {
this.lambda = getAWSLambda();
}
async invoke(params: ServerlessParams): Promise<ServerlessResponse> {
const response = await this.lambda.invoke({
// ...
}).promise();
return {};
}
}
// file: unit.test.ts
import sinon from 'sinon';
import * as lambda from './lambda';
describe('Lambda Unit Test', () => {
let lambdaService: lambda.AWSLambda;
let sandbox: sinon.SinonSandbox;
let getAWSLambdaStub: sinon.SinonStub;
let invokeStub: sinon.SinonStub;
before(() => {
sandbox = sinon.createSandbox();
});
beforeEach(() => {
invokeStub = sandbox.stub();
getAWSLambdaStub = sandbox.stub(lambda, 'getAWSLambda');
getAWSLambdaStub.returns({
invoke: () => ({
promise: invokeStub,
}),
});
lambdaService = new lambda.AWSLambda();
});
it('shound succeed to invoke', async () => {
invokeStub.resolves({
StatusCode: 200,
});
// -----------
// called real function, stub failed.
const res = await lambdaService.invoke();
expect(getAWSLambdaStub).to.be.calledOnce;
expect(invokeStub).to.be.calledOnce;
});
});
The stub failed, the real function was called.
- sinon: 11.1.2
- ts-node: 10.2.1
- typescript: 3.9.7
- node: 14.17.3
tsconfig.json:
{
"compilerOptions": {
"preserveConstEnums": true,
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strict": true,
"moduleResolution": "node",
"sourceMap": false,
"outDir": "dist/src",
"allowJs": true,
"checkJs": true,
"noUnusedLocals": true,
"skipLibCheck": true
},
"ts-node": {
"transpileOnly": true,
"transpiler": "ts-node/transpilers/swc-experimental"
}
}
Everything is ok when my ts-node version is 8.x ~ 9.x , but sinon stub failed when ts-node is v10. Is there any change in the import of module for ts-node's swc ?
Add swc integration and new --transpiler option to use third-party transpilers for a massive speed boost on large codebases
I guess here is the cause of the problem, and I want to use ts-node v10, but I don't known how to fix the problems.