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

chore: add tests #4

Merged
merged 1 commit into from
Jan 25, 2023
Merged
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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"scripts": {
"dev": "tsup --watch --onSuccess \"node dist/bin.js\"",
"build": "tsup",
"test": "vitest run",
"test:watch": "vitest",
"format": "prettier --write --plugin-search-dir=. \"**/*.{js,ts,json}\"",
"format:check": "prettier --check --plugin-search-dir=. \"**/*.{js,ts,json}\"",
"lint": "eslint **/*.{js,ts}",
Expand Down Expand Up @@ -45,10 +47,12 @@
"lint-staged": "^13.1.0",
"prettier": "^2.8.3",
"tsup": "^6.5.0",
"typescript": "^4.9.4"
"typescript": "^4.9.4",
"vitest": "^0.28.1"
},
"dependencies": {
"chalk": "^5.2.0",
"execa": "^6.1.0",
"figlet": "^1.5.2",
"gradient-string": "^2.0.2",
"merge": "^2.1.1",
Expand Down
102 changes: 102 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { execa } from 'execa';
import fs from 'fs';
import path from 'path';
import { afterAll, beforeAll, describe, test } from 'vitest';

import { create } from '../src';
import type { Lang } from '../src/types';
import { mkdir, readJson } from '../src/utils';

const testDir = path.join(__dirname, '.tests-temp');

beforeAll(async () => {
await mkdir(testDir);
});
afterAll(() => {
fs.rmSync(testDir, { recursive: true, force: true });
});

describe('Api Server', () => {
const dir = path.join(__dirname, '../src/templates/server');

for (const lang of fs.readdirSync(dir)) {
describe(lang, async () => {
for (const template of fs.readdirSync(`${dir}/${lang}`)) {
test(template, async () => {
const cwd = path.join(testDir, `${lang}-${template}`);
await create(cwd, {
lang: lang as Lang,
template,
name: `create-nodejs-app-test-${lang}-${template}`,
type: 'server',
eslint: true,
prettier: true,
lintstaged: true,
commitlint: true,
});

// for `husky install` to work
await execa('git init', {
cwd,
});

await execa('pnpm install', {
cwd,
});

if (fs.existsSync(path.join(cwd, 'prisma'))) {
await execa('pnpm prisma db push', {
cwd,
});
}

const scriptsToTest = ['test', 'format:check', 'lint'];

const pkg = readJson(path.join(cwd, 'package.json'));

for (const script of scriptsToTest.filter((s) => !!pkg.scripts[s])) {
await execa(`pnpm ${script}`, {
cwd,
});
}
});
}
});
}
});

describe('Library', () => {
const dir = path.join(__dirname, '../src/templates/library');

for (const lang of fs.readdirSync(dir)) {
test(lang, async () => {
const cwd = path.join(testDir, `library-${lang}`);
await create(cwd, {
lang: lang as Lang,
template: '',
name: `create-nodejs-app-test-library-${lang}`,
type: 'library',
eslint: true,
prettier: true,
});

await execa('git init', {
cwd,
});

await execa('pnpm install', {
cwd,
});

const scriptsToTest = ['test', 'format:check', 'lint', 'build'];

const pkg = readJson(path.join(cwd, 'package.json'));

for (const script of scriptsToTest.filter((s) => !!pkg.scripts[s])) {
await execa(`pnpm ${script}`, {
cwd,
});
}
});
}
});
9 changes: 9 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
globals: true,
include: ['tests/**/*.{test,spec}.{js,ts}'],
testTimeout: 500000,
},
});