Skip to content

Instantly share code, notes, and snippets.

View qunabu's full-sized avatar

Mateusz Wojczal qunabu

View GitHub Profile
@qunabu
qunabu / app.test.js
Created April 19, 2020 18:35
Strapi simple unit test
const request = require("supertest");
// function from gist file
const { setupStrapi } = require("./helpers/strapi");
// We're setting timeout because sometimes bootstrap can take 5-7 seconds (big apps)
jest.setTimeout(10000);
let app; // this is instance of the the strapi
beforeAll(async () => {
@qunabu
qunabu / .gitlab-ci.yml
Created April 19, 2020 18:59
Strapi gitlab testing
image: node:alpine
test:
stage: test
image: node:alpine
before_script:
- npm install
script:
- npm run test-ci
artifacts:
/**
* Default data that factory use
*/
const defaultData = {
password: "1234Abc",
provider: "local",
confirmed: true,
};
/**
* Returns random username object for user creation
const Strapi = require("strapi");
const http = require("http");
let instance;
jest.setTimeout(30000);
/**
* Setups strapi for futher testing
*/
async function setupStrapi() {
{
"routes": [
{
"method": "GET",
"path": "/hi",
"handler": "Hello.hi",
"config": {
"policies": ["plugins::users-permissions.isAuthenticated"]
}
}
module.exports = {
hi: (ctx) => {
ctx.send(`Hi ${ctx.state.user.username}`);
},
};
it("should return `Hi ${user.username}`", async (done) => {
const token = await jwt(user.id);
await request(strapi.server) // app server is and instance of Class: http.Server
.get("/hi")
.set("Authorization", "Bearer " + token)
.expect(200) // Expect response http code 200
.then((data) => {
expect(data.text).toBe(`Hi ${user.username}`); // expect the response welcome text
});
done();
beforeAll(async (done) => {
user = await userFactory.createUser(strapi);
await grantPrivilage(1, "permissions.application.controllers.hello.hi");
done();
});
module.exports = ({ env }) => ({
email: {
provider: "mocknodemailer",
providerOptions: {},
settings: {},
},
});
const request = require("supertest");
const userFactory = require("./../user/factory");
const { jwt, grantPrivilage } = require("./../helpers/strapi");
describe("Hello methods", () => {
let user;
beforeAll(async (done) => {
user = await userFactory.createUser(strapi);
await grantPrivilage(1, "permissions.application.controllers.hello.hi"); // 1 is default role for new confirmed users
done();