Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: init #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v2

- name: Run a one-line script
run: yarn && yarn build
run: yarn && yarn build && yarn test

- name: Conventional Changelog Action
id: changelog
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/simple-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
- uses: actions/checkout@v2

- name: Run a one-line script
run: yarn && yarn build
run: yarn && yarn build && yarn test
19 changes: 19 additions & 0 deletions config/env/test/database.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "bookshelf",
"settings": {
"client": "sqlite",
"filename": ".tmp/test.db"
},
"options": {
"useNullAsDefault": true,
"pool": {
"min": 0,
"max": 1
}
}
}
}
}
19 changes: 15 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
"start": "strapi start",
"build": "strapi build",
"strapi": "strapi",
"commit": "cz"
"commit": "cz",
"test": "jest --forceExit --detectOpenHandles"
},
"devDependencies": {
"cz-conventional-changelog": "3.3.0"
"cz-conventional-changelog": "3.3.0",
"jest": "26.6.3",
"supertest": "6.1.3"
},
"dependencies": {
"@purest/config": "1.0.1",
Expand All @@ -38,12 +41,20 @@
},
"engines": {
"node": ">=10.16.0 <=14.x.x",
"npm": "^7.0.0"
"npm": "7.0.0"
},
"license": "MIT",
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"jest": {
"testPathIgnorePatterns": [
"/node_modules/",
".tmp",
".cache"
],
"testEnvironment": "node"
}
}
}
27 changes: 27 additions & 0 deletions tests/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const fs = require("fs");
const { setupStrapi } = require("./helpers/strapi");

jest.setTimeout(30000);

beforeAll(async (done) => {
await setupStrapi();
done();
});

afterAll(async (done) => {
const dbSettings = strapi.config.get("database.connections.default.settings");

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("./folders/foldertree");
47 changes: 47 additions & 0 deletions tests/folders/foldertree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//TODO / IN PROGRESS

const request = require("supertest");

const jwt =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNjE1OTgxODU5LCJleHAiOjE2MTg1NzM4NTl9.06YN9zo3e5sg9zmuuRj-UvmoabMHLClcK1jpb6iKlH4";

// const mockUserData = {
// username: "tester",
// email: "[email protected]",
// provider: "local",
// password: "1234abc",
// confirmed: true,
// blocked: null,
// };

// const getJwt = async () => {
// const defaultRole = await strapi
// .query("role", "users-permissions")
// .findOne({ id: 1 }, []);

// const role = defaultRole ? defaultRole.id : null;
// /** Creates a new user an push to database */
// const user = await strapi.plugins["users-permissions"].services.user.add({
// ...mockUserData,
// role,
// });

// const jwt = strapi.plugins["users-permissions"].services.jwt.issue({
// id: user.id,
// });
// return jwt;
// };

it("should return a folder tree", async (done) => {
// const jwt = await getJwt();
await request(strapi.server) // app server is an instance of Class: http.Server
.get("/folders/tree")
.set("accept", "application/json")
.set("Content-Type", "application/json")
.set("Authorization", "Bearer " + jwt)
//.expect(200) // Expect response http code 200
.then((data) => {
expect(data.text).toBe("Hello World!"); // expect the response text
});
done();
});
18 changes: 18 additions & 0 deletions tests/helpers/strapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Strapi = require("strapi");
const http = require("http");

let instance;

async function setupStrapi() {
if (!instance) {
await Strapi().load();
instance = strapi;
await instance.app
.use(instance.router.routes())
.use(instance.router.allowedMethods());

instance.server = http.createServer(instance.app.callback());
}
return instance;
}
module.exports = { setupStrapi };
Loading