Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"tsx": "4.17.0",
"typescript": "5.5.4",
"vite-tsconfig-paths": "5.1.4",
"vitest": "3.2.4",
"vitest": "4.0.4",
"wrangler": "4.1.0"
},
"lint-staged": {
Expand Down
69 changes: 40 additions & 29 deletions packages/graphql-codegen-cli/tests/generate-and-save.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,25 @@ const SIMPLE_TEST_SCHEMA = `type MyType { f: String } type Query { f: String }`;
const inputFile = join(__dirname, '../temp/input-graphql.tsx');
const outputFile = join(__dirname, '../temp/output-graphql.tsx');

const writeSpy = vi.spyOn(fs, 'writeFile');
const readSpy = vi.spyOn(fs, 'readFile');
const outputErrorSpy = vi.spyOn(process.stderr, 'write');

describe('generate-and-save', () => {
beforeEach(() => {
// We must call .spyOn and .mockReset these individually instead of vi.resetAllMocks
// because there's a `vi.spyOn(process, 'cwd').mockImplementation(() => __dirname);` in vitest.setup.ts
//
// If we called vi.resetAllMocks, the cwd spy is reset too!
// That would cause all `schema` and `documents`paths to be from the root of the workspace
writeSpy.mockReset();
readSpy.mockReset();
outputErrorSpy.mockReset();
});

test('allow to specify overwrite for specific output (should write file)', async () => {
const filename = 'overwrite.ts';
const writeSpy = vi.spyOn(fs, 'writeFile').mockImplementation(() => Promise.resolve());
writeSpy.mockImplementation(() => Promise.resolve());

const output = await generate(
{
Expand All @@ -42,10 +57,8 @@ describe('generate-and-save', () => {

test('allow to specify overwrite for specific output (should not write file)', async () => {
const filename = 'overwrite.ts';
const writeSpy = vi.spyOn(fs, 'writeFile').mockImplementation(() => Promise.resolve());
// forces file to exist
const fileReadSpy = vi.spyOn(fs, 'readFile');
fileReadSpy.mockImplementation(async () => '');
writeSpy.mockImplementation(() => Promise.resolve());
readSpy.mockImplementation(async () => ''); // forces file to exist

const output = await generate(
{
Expand All @@ -66,14 +79,14 @@ describe('generate-and-save', () => {

expect(output.length).toBe(1);
// makes sure it checks if file is there
expect(fileReadSpy).toHaveBeenCalledWith(filename);
expect(readSpy).toHaveBeenCalledWith(filename);
// makes sure it doesn't write a new file
expect(writeSpy).not.toHaveBeenCalled();
});

test('should use global overwrite option and write a file', async () => {
const filename = 'overwrite.ts';
const writeSpy = vi.spyOn(fs, 'writeFile').mockImplementation(() => Promise.resolve());
writeSpy.mockImplementation(() => Promise.resolve());

const output = await generate(
{
Expand All @@ -98,10 +111,8 @@ describe('generate-and-save', () => {

test('should use global overwrite option and not write a file', async () => {
const filename = 'overwrite.ts';
const writeSpy = vi.spyOn(fs, 'writeFile').mockImplementation(() => Promise.resolve());
// forces file to exist
const fileReadSpy = vi.spyOn(fs, 'readFile');
fileReadSpy.mockImplementation(async () => '');
writeSpy.mockImplementation(() => Promise.resolve());
readSpy.mockImplementation(async () => ''); // forces file to exist

const output = await generate(
{
Expand All @@ -121,15 +132,15 @@ describe('generate-and-save', () => {

expect(output.length).toBe(1);
// makes sure it checks if file is there
expect(fileReadSpy).toHaveBeenCalledWith(filename);
expect(readSpy).toHaveBeenCalledWith(filename);
// makes sure it doesn't write a new file
expect(writeSpy).not.toHaveBeenCalled();
});

test('should overwrite a file by default', async () => {
const filename = 'overwrite.ts';
const writeSpy = vi.spyOn(fs, 'writeFile').mockImplementation(() => Promise.resolve());
const readSpy = vi.spyOn(fs, 'readFile').mockImplementation(() => Promise.resolve(''));
writeSpy.mockImplementation(() => Promise.resolve());
readSpy.mockImplementation(() => Promise.resolve(''));
readSpy.mockImplementation(async _f => '');

const output = await generate(
Expand Down Expand Up @@ -189,11 +200,11 @@ describe('generate-and-save', () => {
});
test('should extract a document from the gql tag (imported from apollo-server)', async () => {
const filename = 'overwrite.ts';
const writeSpy = vi.spyOn(fs, 'writeFile').mockImplementation(() => Promise.resolve());
writeSpy.mockImplementation(() => Promise.resolve());

const output = await generate(
{
schema: `./tests/test-files/schema-dir/gatsby-and-custom-parsers/apollo-server.ts`,
schema: './tests/test-files/schema-dir/gatsby-and-custom-parsers/apollo-server.ts',
generates: {
[filename]: {
plugins: ['typescript'],
Expand All @@ -210,7 +221,7 @@ describe('generate-and-save', () => {
});
test('should allow to alter the content with the beforeOneFileWrite hook', async () => {
const filename = 'modify.ts';
const writeSpy = vi.spyOn(fs, 'writeFile').mockImplementation(() => Promise.resolve());
writeSpy.mockImplementation(() => Promise.resolve());

const output = await generate(
{
Expand Down Expand Up @@ -249,7 +260,7 @@ describe('generate-and-save', () => {

test('Schema syntax error - should print native GraphQLError', async () => {
expect.assertions(1);
const outputErrorMock = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
outputErrorSpy.mockImplementation(() => true);
try {
await generate(
{
Expand All @@ -265,7 +276,7 @@ describe('generate-and-save', () => {
);
} catch {
const cwd = process.cwd(); // cwd is different for every machine, remember to replace local path with this after updating snapshot
expect(outputErrorMock.mock.calls[0][0]).toMatchInlineSnapshot(`
expect(outputErrorSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
"[FAILED] Failed to load schema from ./tests/test-files/schema-dir/error-schema.graphql:
[FAILED] Syntax Error: Expected Name, found "!".

Expand All @@ -292,7 +303,7 @@ describe('generate-and-save', () => {

test('Document syntax error - should print native GraphQLError', async () => {
expect.assertions(1);
const outputErrorMock = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
outputErrorSpy.mockImplementation(() => true);
try {
await generate(
{
Expand All @@ -309,7 +320,7 @@ describe('generate-and-save', () => {
);
} catch {
const cwd = process.cwd(); // cwd is different for every machine, remember to replace local path with this after updating snapshot
expect(outputErrorMock.mock.calls[0][0]).toMatchInlineSnapshot(`
expect(outputErrorSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
"[FAILED] Failed to load documents from ./tests/test-files/error-document.graphql:
[FAILED] Syntax Error: Expected "{", found <EOF>.

Expand All @@ -324,7 +335,7 @@ describe('generate-and-save', () => {

test('No documents found - should throw error by default', async () => {
expect.assertions(1);
const outputErrorMock = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
outputErrorSpy.mockImplementation(() => true);
try {
await generate(
{
Expand All @@ -340,7 +351,7 @@ describe('generate-and-save', () => {
false
);
} catch {
expect(outputErrorMock.mock.calls[0][0]).toMatchInlineSnapshot(`
expect(outputErrorSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
"
[FAILED] Unable to find any GraphQL type definitions for the following pointers:
[FAILED] - ./tests/test-files/document-file-does-not-exist.graphql
Expand All @@ -350,7 +361,7 @@ describe('generate-and-save', () => {
});

test('No documents found - should not fail if ignoreNoDocuments=true', async () => {
const outputErrorMock = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
outputErrorSpy.mockImplementation(() => true);
await generate(
{
verbose: true,
Expand All @@ -365,11 +376,11 @@ describe('generate-and-save', () => {
},
false
);
expect(outputErrorMock).not.toHaveBeenCalled();
expect(outputErrorSpy).not.toHaveBeenCalled();
});

test('No documents found - GraphQL Config - should throw error by default', async () => {
const outputErrorMock = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
outputErrorSpy.mockImplementation(() => true);
expect.assertions(1);
try {
const config = await createContext({
Expand All @@ -385,7 +396,7 @@ describe('generate-and-save', () => {

await generate(config, false);
} catch {
expect(outputErrorMock.mock.calls[0][0]).toMatchInlineSnapshot(`
expect(outputErrorSpy.mock.calls[0][0]).toMatchInlineSnapshot(`
"
[FAILED] Unable to find any GraphQL type definitions for the following pointers:
[FAILED] - ../test-documents/empty.graphql
Expand All @@ -395,7 +406,7 @@ describe('generate-and-save', () => {
});

test('No documents found - GraphQL Config - should not fail if ignoreNoDocuments=true', async () => {
const outputErrorMock = vi.spyOn(process.stderr, 'write').mockImplementation(() => true);
outputErrorSpy.mockImplementation(() => true);
vi.spyOn(fs, 'writeFile').mockImplementation(() => Promise.resolve());
const config = await createContext({
config: './tests/test-files/graphql.config.no-doc-ignored.js',
Expand All @@ -410,7 +421,7 @@ describe('generate-and-save', () => {

await generate(config, false);

expect(outputErrorMock).not.toHaveBeenCalled();
expect(outputErrorSpy).not.toHaveBeenCalled();
});
});

Expand Down
Loading
Loading