System Information
- Strapi Version: 3.3.4
- Operating System:
- Database: mongo
- Node Version: 12.16.1
- NPM Version:
- Yarn Version: 1.19.1
I there,
I’m tying to integrate unit testing to my strapi application. I follow these documentation :
But when i run a test I have a 403 and not a 200
/tests/app.test.js
const fs = require('fs') const { setupStrapi } = require('./helpers/strapi') /** this code is called once before any test is called */ jest.setTimeout(15000) beforeAll(async (done) => { await setupStrapi() // singleton so it can be called many times done() }) /** this code is called once before all the tested are finished */ afterAll(async (done) => { const dbSettings = strapi.config.get('database.connections.default.settings') //delete test database after all tests if (dbSettings && dbSettings.filename) { const tmpDbFile = `${__dirname}/../${dbSettings.filename}` if (fs.existsSync(tmpDbFile)) { fs.unlinkSync(tmpDbFile) } } done() }) it('strapi is defined', () => { expect(strapi).toBeDefined() }) require('./hello')
/test/helpers/strapi.js
const Strapi = require('strapi') const http = require('http') let instance async function setupStrapi() { if (!instance) { /** the following code in copied from `./node_modules/strapi/lib/Strapi.js` */ await Strapi().load() instance = strapi // strapi is global now await instance.app .use(instance.router.routes()) // populate KOA routes .use(instance.router.allowedMethods()) // populate KOA methods instance.server = http.createServer(instance.app.callback()) } return instance } module.exports = { setupStrapi }
/tests/hello/index.js
const request = require('supertest') it('should return hello world', async (done) => { await request(strapi.server) // app server is an instance of Class: http.Server .get('/hello') .expect(200) // Expect response http code 200 .then((data) => { expect(data.text).toBe('Hello World!') // expect the response text }) done() })
Any idea ?
Thanks in advance