I’m trying to use Sinon with Mocha and Chai for testing in a TypeScript project, but TypeScript gives me the following error:
TS1215: Invalid use of ‘arguments’. Modules are automatically in strict mode.
Looking at the code that it’s talking about, the error makes sense, as “arguments” is already reserved, so you couldn’t have a variable named that.
/**
* @callback NextTick
* @param {VoidVarArgsFunc} callback - the callback to run
* @param {...*} arguments - optional arguments to call the callback with
* @returns {void}
*/
/**
* @callback SetImmediate
* @param {VoidVarArgsFunc} callback - the callback to run
* @param {...*} arguments - optional arguments to call the callback with
* @returns {NodeImmediate}
*/
You’d think, then, that this would be a common issue, but I’m seeing nothing in my searches related to this.
Here’s my tsconfig.json
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"removeComments": true,
"sourceMap": true,
"target": "ES5",
"module": "ESNext",
"moduleResolution": "node",
"allowJs": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": false,
"emitDecoratorMetadata": false,
"importHelpers": true,
"downlevelIteration": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"verbatimModuleSyntax": true,
"resolveJsonModule": true,
"baseUrl": ".",
"paths": {
"@app*": [ "./src/app/*", "./src/app/*/index" ],
"@lib*": [ "./src/lib/*", "./src/lib/*/index" ],
"@components*": [ "./src/app/components/*", "./src/app/components/*/index" ]
},
"lib": [
"dom",
"es5",
"es2015.core",
"es2015.collection",
"es2015.reflect",
"es2015.iterable",
"es2015.symbol",
"es2015.symbol.wellknown"
]
},
"exclude": [
"dist",
"static",
"node_modules",
"webpack.config.js"
],
"compileOnSave": false
}
And the test file (ignore use of nameof, it’s an additional transformer that functions similarly to C#’s nameof keyword)
import { expect } from "chai";
import { createStubInstance, stub, spy, type SinonStub } from "sinon";
import { RequestService } from "./RequestService";
import { SecurityService } from "./SecurityService";
describe("app", () => {
describe("services", () => {
describe(nameof(RequestService), () => {
let originalFetch = fetch;
let requestService: RequestService | undefined;
let fetchStub: SinonStub<[input: RequestInfo | URL, init?: RequestInit | undefined], Promise<Response>> | undefined;
suiteSetup(() => {
fetchStub = stub(window, "fetch");
fetchStub.returns(Promise.resolve(new Response("[]", { status: 200, headers: { "Content-type": "application/json" } })))
requestService = new RequestService(createStubInstance(SecurityService));
});
suiteTeardown(() => {
window["fetch"] = originalFetch;
});
it("should exist", () => {
expect(RequestService).to.exist;
});
it("should make requests using fetch", () => {
const fetchSpy = spy(fetchStub);
requestService?.get("http://foo.bar.baz");
expect(fetchSpy?.called).to.be.true;
});
});
});
})
I also have @types/sinon installed as a dev dependency.