Skip to content

Commit

Permalink
Remove some hacks from the cli tests so they work with esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Sep 9, 2022
1 parent ee0c492 commit d42708a
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 183 deletions.
27 changes: 6 additions & 21 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ module.exports = {
node: true,
jest: true,
},
plugins: [
'react',
'react-hooks',
'jest',
'import',
'@typescript-eslint',
'@preconstruct/format-js-tag',
],
plugins: ['react', 'react-hooks', 'jest', 'import', '@typescript-eslint'],
settings: {
react: {
version: 'detect',
Expand Down Expand Up @@ -103,31 +96,23 @@ module.exports = {
},
},
],
'@preconstruct/format-js-tag/format': 'error',
},
extends: ['plugin:jest/recommended'],

// Disable some rules for (code blocks within) Markdown docs
overrides: [
{
files: ['**/*.md'],
files: ['**/*.{ts,tsx}'],
rules: {
'no-unused-vars': 'off',
// TypeScript already checks for the following things and they conflict with TypeScript
'import/no-unresolved': 'off',
'no-undef': 'off',
},
},
{
files: ['packages/fields/src/**/*.{js,ts,tsx}'],
rules: {
'import/no-commonjs': 'error',
},
},
{
files: ['**/*.{ts,tsx}'],
files: ['packages/core/src/scripts/tests/fixtures/**/*.{ts,tsx}'],
rules: {
// TypeScript already checks for the following things and they conflict with TypeScript
'import/no-unresolved': 'off',
'no-undef': 'off',
'import/no-extraneous-dependencies': 'off',
},
},
],
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"@jest/test-sequencer": "^29.0.0",
"@manypkg/cli": "^0.19.1",
"@preconstruct/cli": "2.2.1",
"@preconstruct/eslint-plugin-format-js-tag": "^0.2.0",
"@testing-library/jest-dom": "^5.15.0",
"@types/babel__core": "^7.1.16",
"@types/jest": "^29.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
"fast-glob": "^3.2.7",
"fixturez": "^1.1.0",
"mime": "^3.0.0",
"outdent": "^0.8.0",
"string-argv": "^0.3.1",
"strip-ansi": "^6.0.1"
},
Expand Down
51 changes: 15 additions & 36 deletions packages/core/src/scripts/tests/artifacts.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { text } from '../../fields';
import { config, list } from '../..';
import fs from 'fs/promises';
import { ExitError } from '../utils';
import { allowAll } from '../../access';
import {
basicKeystoneConfig,
getFiles,
recordConsole,
runCommand,
Expand All @@ -11,21 +10,6 @@ import {
testdir,
} from './utils';

const basicKeystoneConfig = {
kind: 'config' as const,
config: config({
db: { provider: 'sqlite', url: 'file:./app.db' },
lists: {
Todo: list({
access: allowAll,
fields: {
title: text(),
},
}),
},
}),
};

describe.each(['postinstall', 'build', 'prisma migrate status'])('%s', command => {
test('logs an error and exits with 1 when the schemas do not exist and the terminal is non-interactive', async () => {
const tmp = await testdir({
Expand All @@ -41,6 +25,8 @@ describe.each(['postinstall', 'build', 'prisma migrate status'])('%s', command =
});
});

const schemasMatch = ['schema.prisma', 'schema.graphql'];

// a lot of these cases are also the same for prisma and build commands but we don't include them here
// because when they're slow and then run the same code as the postinstall command
// (and in the case of the build command we need to spawn a child process which would make each case take a _very_ long time)
Expand All @@ -54,12 +40,8 @@ describe('postinstall', () => {
'Would you like to update your Prisma and GraphQL schemas?': true,
});
await runCommand(tmp, 'postinstall');
const files = await getFiles(tmp, ['schema.prisma', 'schema.graphql']);
// to update them
// for (const [file, content] of Object.entries(files)) {
// require('fs').writeFileSync(`${__dirname}/fixtures/basic-project/${file}`, content);
// }
expect(files).toEqual(await getFiles(`${__dirname}/fixtures/basic-project`));
const files = await getFiles(tmp, schemasMatch);
expect(files).toEqual(await getFiles(`${__dirname}/fixtures/basic-project`, schemasMatch));
expect(recording()).toMatchInlineSnapshot(`
"Your Prisma and GraphQL schemas are not up to date
Prompt: Would you like to update your Prisma and GraphQL schemas? true
Expand All @@ -73,8 +55,8 @@ describe('postinstall', () => {
});
const recording = recordConsole();
await runCommand(tmp, 'postinstall --fix');
const files = await getFiles(tmp, ['schema.prisma', 'schema.graphql']);
expect(files).toEqual(await getFiles(`${__dirname}/fixtures/basic-project`));
const files = await getFiles(tmp, schemasMatch);
expect(files).toEqual(await getFiles(`${__dirname}/fixtures/basic-project`, schemasMatch));
expect(recording()).toMatchInlineSnapshot(`"✨ Generated GraphQL and Prisma schemas"`);
});
test("does not prompt, error or modify the schemas if they're already up to date", async () => {
Expand All @@ -85,8 +67,8 @@ describe('postinstall', () => {
});
const recording = recordConsole();
await runCommand(tmp, 'postinstall');
const files = await getFiles(tmp, ['schema.prisma', 'schema.graphql']);
expect(files).toEqual(await getFiles(`${__dirname}/fixtures/basic-project`));
const files = await getFiles(tmp, schemasMatch);
expect(files).toEqual(await getFiles(`${__dirname}/fixtures/basic-project`, schemasMatch));
expect(recording()).toMatchInlineSnapshot(`"✨ GraphQL and Prisma schemas are up to date"`);
});
test('writes the correct node_modules files', async () => {
Expand All @@ -104,10 +86,7 @@ describe('postinstall', () => {
const tmp = await testdir({
...symlinkKeystoneDeps,
...schemas,
'keystone.js': {
kind: 'config',
config: { ...basicKeystoneConfig.config, experimental: { generateNodeAPI: true } },
},
'keystone.js': await fs.readFile(`${__dirname}/fixtures/generate-node-api.ts`, 'utf8'),
});
const recording = recordConsole();
await runCommand(tmp, 'postinstall');
Expand Down Expand Up @@ -136,10 +115,10 @@ describe('postinstall', () => {
const tmp = await testdir({
...symlinkKeystoneDeps,
...schemas,
'keystone.js': {
kind: 'config',
config: { ...basicKeystoneConfig.config, experimental: { generateNextGraphqlAPI: true } },
},
'keystone.js': await fs.readFile(
`${__dirname}/fixtures/generate-next-graphql-api.ts`,
'utf8'
),
});
const recording = recordConsole();
await runCommand(tmp, 'postinstall');
Expand Down
24 changes: 4 additions & 20 deletions packages/core/src/scripts/tests/build.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import execa from 'execa';
import * as fs from 'fs-extra';
import stripAnsi from 'strip-ansi';
import { ExitError } from '../utils';
import {
basicKeystoneConfig,
cliBinPath,
js,
recordConsole,
runCommand,
schemas,
Expand Down Expand Up @@ -36,23 +36,7 @@ test('build works with typescript without the user defining a babel config', asy
const tmp = await testdir({
...symlinkKeystoneDeps,
...schemas,
'keystone.ts': js`
import { config, list } from "@keystone-6/core";
import { text } from "@keystone-6/core/fields";
type x = string;
export default config({
db: { provider: "sqlite", url: "file:./app.db" },
lists: {
Todo: list({
fields: {
title: text(),
},
}),
},
});
`,
'keystone.ts': await fs.readFile(`${__dirname}/fixtures/with-ts.ts`, 'utf8'),
});
const result = await execa('node', [cliBinPath, 'build'], {
reject: false,
Expand All @@ -64,8 +48,8 @@ test('build works with typescript without the user defining a babel config', asy
});
expect(await fs.readFile(`${tmp}/node_modules/.keystone/types.js`, 'utf8')).toBe('');
expect(
result
.all!.replace(
stripAnsi(result.all!)
.replace(
'\nwarn - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache',
''
)
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/scripts/tests/fixtures/basic-project/keystone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { list, config } from '@keystone-6/core';
import { allowAll } from '@keystone-6/core/access';
import { text } from '@keystone-6/core/fields';

export default config({
db: {
provider: 'sqlite',
url: 'file:./app.db',
},
lists: {
Todo: list({
access: allowAll,
fields: {
title: text(),
},
}),
},
});
19 changes: 19 additions & 0 deletions packages/core/src/scripts/tests/fixtures/basic-with-no-ui.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { list, config } from '@keystone-6/core';
import { allowAll } from '@keystone-6/core/access';
import { text } from '@keystone-6/core/fields';

export default config({
db: {
provider: 'sqlite',
url: 'file:./app.db',
},
ui: { isDisabled: true },
lists: {
Todo: list({
access: allowAll,
fields: {
title: text(),
},
}),
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { list, config } from '@keystone-6/core';
import { allowAll } from '@keystone-6/core/access';
import { text } from '@keystone-6/core/fields';

export default config({
db: {
provider: 'sqlite',
url: 'file:./app.db',
},
lists: {
Todo: list({
access: allowAll,
fields: {
title: text(),
},
}),
},
experimental: {
generateNextGraphqlAPI: true,
},
});
21 changes: 21 additions & 0 deletions packages/core/src/scripts/tests/fixtures/generate-node-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { list, config } from '@keystone-6/core';
import { allowAll } from '@keystone-6/core/access';
import { text } from '@keystone-6/core/fields';

export default config({
db: {
provider: 'sqlite',
url: 'file:./app.db',
},
lists: {
Todo: list({
access: allowAll,
fields: {
title: text(),
},
}),
},
experimental: {
generateNodeAPI: true,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { list, config } from '@keystone-6/core';
import { allowAll } from '@keystone-6/core/access';

export default config({
db: {
provider: 'sqlite',
url: 'file:./app.db',
useMigrations: true,
},
ui: { isDisabled: true },
lists: {
Todo: list({
access: allowAll,
fields: {},
}),
},
});
16 changes: 16 additions & 0 deletions packages/core/src/scripts/tests/fixtures/no-fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { list, config } from '@keystone-6/core';
import { allowAll } from '@keystone-6/core/access';

export default config({
db: {
provider: 'sqlite',
url: 'file:./app.db',
},
ui: { isDisabled: true },
lists: {
Todo: list({
access: allowAll,
fields: {},
}),
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { list, config } from '@keystone-6/core';
import { allowAll } from '@keystone-6/core/access';
import { text } from '@keystone-6/core/fields';

export default config({
db: {
provider: 'sqlite',
url: 'file:./app.db',
useMigrations: true,
},
ui: { isDisabled: true },
lists: {
Todo: list({
access: allowAll,
fields: {
title: text(),
},
}),
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { list, config } from '@keystone-6/core';
import { allowAll } from '@keystone-6/core/access';
import { checkbox, text } from '@keystone-6/core/fields';

export default config({
db: {
provider: 'sqlite',
url: 'file:./app.db',
useMigrations: true,
},
ui: { isDisabled: true },
lists: {
Todo: list({
access: allowAll,
fields: {
title: text(),
isComplete: checkbox(),
},
}),
},
});
Loading

0 comments on commit d42708a

Please sign in to comment.