forked from keystonejs/create-keystone-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
188 lines (164 loc) · 5.94 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import * as playwright from 'playwright';
import { execFile } from 'child_process';
import tempy from 'tempy';
import path from 'path';
import { promisify } from 'util';
import _treeKill from 'tree-kill';
import retry from 'async-retry';
import { randomBytes } from 'crypto';
const treeKill = promisify(_treeKill);
const execFileAsync = promisify(execFile);
// some tests are slow
jest.setTimeout(100000);
// WARNING:
// the order of tests is important, and why we use --runInBand
// `keystone dev` creates the database for production
// playwright tips
// for debugging, you'll want to set the env var PWDEBUG=1
// this will open the browser in a headed mode and open the inspector
// https://playwright.dev/docs/inspector
let projectDir = path.join(__dirname, 'create-keystone-app', 'starter');
if (process.env.TEST_MATRIX_NAME === 'cka') {
test('can create a basic project', async () => {
const cwd = tempy.directory();
const createKeystoneAppProcess = execFileAsync(
'node',
[require.resolve('./create-keystone-app/bin.js'), 'test-project'],
{ cwd }
);
createKeystoneAppProcess.child.stdout!.pipe(process.stdout);
await createKeystoneAppProcess;
projectDir = path.join(cwd, 'test-project');
});
}
async function startKeystone(
command: 'start' | 'dev',
env: Record<string, any> = process.env
) {
const keystoneProcess = execFile('yarn', [command], {
cwd: projectDir,
env,
});
keystoneProcess.stdout!.pipe(process.stdout);
const adminUIReady = new Promise((resolve, reject) => {
keystoneProcess.stdout!.on('data', (buffer: Buffer) => {
if (buffer.toString('utf-8').includes('Admin UI ready')) {
return resolve(true);
}
});
});
const cleanupKeystoneProcess = async () => {
keystoneProcess.stdout!.unpipe(process.stdout);
// childProcess.kill will only kill the direct child process
// so we use tree-kill to kill the process and it's children
if (keystoneProcess.pid) {
await treeKill(keystoneProcess.pid);
}
};
await adminUIReady;
return cleanupKeystoneProcess;
}
describe.each(['development', 'production'] as const)('%s', (mode) => {
let cleanupKeystoneProcess = () => {};
afterAll(async () => {
await cleanupKeystoneProcess();
});
if (mode === 'development') {
// process.env.SESSION_SECRET is randomly generated for this
test('start keystone in dev', async () => {
cleanupKeystoneProcess = await startKeystone('dev');
});
} else if (mode === 'production') {
const env = {
NODE_ENV: 'production',
SESSION_SECRET: randomBytes(32).toString('hex'),
};
test('build keystone', async () => {
const keystoneBuildProcess = execFileAsync('yarn', ['build'], {
cwd: projectDir,
env: {
...process.env,
...env,
},
});
keystoneBuildProcess.child.stdout!.pipe(process.stdout);
keystoneBuildProcess.child.stderr!.pipe(process.stdout);
await keystoneBuildProcess;
});
test('start keystone in prod', async () => {
cleanupKeystoneProcess = await startKeystone('start', {
...process.env,
...env,
});
});
}
describe.each(['chromium'] as const)('%s', (browserName) => {
let page: playwright.Page = undefined as any;
let browser: playwright.Browser = undefined as any;
beforeAll(async () => {
await deleteAllData(projectDir);
browser = await playwright[browserName].launch();
page = await browser.newPage();
page.setDefaultNavigationTimeout(6000);
});
test('create user', async () => {
await page.goto('http://localhost:3000');
await page.fill('label:has-text("Name") >> .. >> input', 'Admin1');
await page.fill(
'label:has-text("Email") >> .. >> input',
);
await page.click('button:has-text("Set Password")');
await page.fill('[placeholder="New Password"]', 'password');
await page.fill('[placeholder="Confirm Password"]', 'password');
await page.click('button:has-text("Get started")');
await page.uncheck('input[type="checkbox"]', { force: true });
await page.click('text=Continue');
});
test('change admin name', async () => {
await page.click('h3:has-text("Users")');
await page.click('a:has-text("Admin1")');
await page.fill('label:has-text("Name") >> .. >> input', 'Admin2');
await page.click('button:has-text("Save changes")');
await page.click('nav >> text=Users');
expect(await page.textContent('a:has-text("Admin2")')).toBe('Admin2');
});
test('create a post', async () => {
await page.click('nav >> text=Posts');
await page.click('a:has-text("Create Post")');
await page.fill('input[type="text"]', 'title');
await page.click('button:has-text("Create Post")');
// await page.waitForTimeout(2000); // TODO: the 'Save changes' button is painful
// await page.fill('input[type="text"]', 'title again');
// await page.click('button:has-text("Save changes")');
});
afterAll(async () => {
await browser.close();
});
});
});
async function deleteAllData(projectDir: string) {
/**
* As of @prisma/[email protected] it appears that the prisma client runtime tries to resolve the path to the prisma schema
* from process.cwd(). This is not always the project directory we want to run keystone from.
* Here we mutate the process.cwd global with a fn that returns the project directory we expect, such that prisma
* can retrieve the correct schema file.
*/
const prevCwd = process.cwd;
try {
process.cwd = () => {
return projectDir;
};
const { PrismaClient } = require(path.join(
projectDir,
'node_modules/.prisma/client'
));
const prisma = new PrismaClient();
await Promise.all(
Object.values(prisma).map((x: any) => x?.deleteMany?.({}))
);
await prisma.$disconnect();
} finally {
process.cwd = prevCwd;
}
}