-
I have a very basic AdonisJS setup and want to write some unit tests. I followed the tutorial but can't get it to work for Controller Unit tests. Here's everything needed to recreate the issue: > yarn add -D japa [email protected] [email protected] ( // japaFile.ts
import { configure } from 'japa'
configure({
files: ['test/**/*.spec.ts'],
}) // App/Util/UUIDGenerator.ts
const createUUID = () => {
return 'Hello'
}
export { createUUID } // test/UUIDGenerator.spec.ts
import test from 'japa'
import { createUUID } from 'App/Util/UUIDGenerator'
test.group('UUIDGenerator', () => {
test('Should create some kind of random uuids', (assert) => {
assert.isTrue(createUUID() === 'Hello')
})
}) // package.json
{
...
"scripts": {
"test": "node -r @adonisjs/assembler/build/register japaFile.ts",
...
},
...
} I get this error when I run the tests: > yarn test
yarn run v1.22.17
$ node -r @adonisjs/assembler/build/register japaFile.ts
TypeError: global[Symbol.for(...)] is not a function
at Object.<anonymous> (/Users/myuser/Projects/adonisproject/test/UUIDGenerator.spec.ts:2:1)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Module._compile (/Users/myuser/Projects/adonisproject/node_modules/pirates/lib/index.js:136:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Object.newLoader [as .ts] (/Users/myuser/Projects/adonisproject/node_modules/pirates/lib/index.js:141:7)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at /Users/myuser/Projects/adonisproject/node_modules/japa/build/src/SlimRunner/index.js:146:16
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
> Is it not possible to create unit tests for utils / controllers? Am I doing something wrong? Thanks in advance for any help 😊 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Make sure to have the part that starts the http server: async function startHttpServer() {
const { Ignitor } = await import('@adonisjs/core/build/src/Ignitor')
process.env.PORT = String(await getPort())
await new Ignitor(__dirname).httpServer().start()
} As this one is the part that sets up the ioc container global bindings ( |
Beta Was this translation helpful? Give feedback.
Make sure to have the part that starts the http server:
As this one is the part that sets up the ioc container global bindings (
global[Symbol.for('ioc.use')]
), required for using the aliases (in your caseApp/Util/UUIDGenerator
)