'Parameter 'obj' implicitly has an 'any' type when using mocha do do unit test
I define a test class in typescript named DeviceHandler.test.ts like this:
import 'mocha'
import { expect } from 'chai'
import DeviceHandler from '@utils/data/DeviceHandler'
describe('Device Test', function () {
it('Get device fingerprints', async function () {
let fingerprints:string = await DeviceHandler.getDeviceId();
expect(fingerprints).to.be.a("d");
})
})
what I want to do is try to test the get fringer prints function works or not. this is the DeviceHandler.ts looks like:
const DeviceHandler = {
getDeviceId: async() : Promise<string> => {
return new Promise((resolve, reject) => {
resolve("xxxx");
});
},
}
export default DeviceHandler;
but when I run this test like this:
"test": "mocha --recursive --require babel-register test/**/** test/utils/data/** --bail",
shows error:
~/source/reddwarf/frontend/js-wheel on main! ⌚ 13:19:47
$ yarn test ‹ruby-2.7.2›
yarn run v1.21.1
$ mocha --recursive --require babel-register test/**/** test/utils/data/** --bail
TSError: ⨯ Unable to compile TypeScript:
test/utils/data/DeviceHandler.test.ts:13:33 - error TS7006: Parameter 'obj' implicitly has an 'any' type.
13 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
~~~
test/utils/data/DeviceHandler.test.ts:15:28 - error TS7006: Parameter 'fn' implicitly has an 'any' type.
why did this problem happen? what should I do to fix this? this is my package.json:
{
"name": "js-wheel",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "mocha --recursive --require babel-register test/**/** test/utils/data/** --bail",
"tscbuild": "ttsc",
"build": "rm -rf dist && webpack --mode development --config src/config/webpack.config.js",
"watch:build": "tsc --watch",
"watch:server": "nodemon './dist/index.js' --watch './dist'",
"start": "npm-run-all clean build --parallel watch:build watch:server --print-label",
"release": "tsc && npm publish"
},
"dependencies": {
"@fingerprintjs/fingerprintjs": "^3.3.2",
"@types/chrome": "^0.0.178",
"@types/uuid": "^8.3.4",
"dayjs": "^1.10.7",
"module-alias": "^2.2.2",
"npm": "^8.4.1",
"rollup-plugin-typescript2": "^0.31.2",
"ts-loader": "^9.2.6",
"ts-transformer-keys": "^0.4.3",
"tsc-alias": "^1.6.0",
"ttypescript": "^1.5.13",
"uuid": "^8.3.2",
"webpack": "^5.68.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jiangxiaoqiang/js-wheel.git"
},
"author": "[email protected]",
"license": "ISC",
"bugs": {
"url": "https://github.com/jiangxiaoqiang/js-wheel/issues"
},
"homepage": "https://github.com/jiangxiaoqiang/js-wheel#readme",
"devDependencies": {
"@types/chai": "^4.2.11",
"@types/expect": "^24.3.0",
"@types/mocha": "^7.0.2",
"@types/node": "^14.0.11",
"@types/sinon": "^10.0.11",
"babel-preset-env": "^1.7.0",
"babel-register": "^6.26.0",
"chai": "^4.2.0",
"cross-env": "^7.0.3",
"mocha": "^7.2.0",
"nyc": "^15.1.0",
"sinon": "^13.0.1",
"ts-jest": "^27.1.3",
"ts-mocha": "^9.0.2",
"ts-node": "^10.6.0",
"ts-patch": "^2.0.1",
"ts-transform-paths": "^2.0.3",
"tsconfig-paths": "^3.12.0",
"typescript": "^4.5.5",
"typescript-transform-paths": "^3.3.1",
"webpack-cli": "^4.9.2"
},
"mocha": {
"require": [
"ts-node/register",
"tsconfig-paths/register"
],
"ui": "bdd"
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
