From aeedfac37e41201dee81fade3e73fad38d79285f Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Wed, 25 Nov 2020 11:12:13 -0500 Subject: [PATCH] Add graphql tests to CI, move snapshots to fixures, use mockdate --- .github/workflows/test.yml | 2 +- package-lock.json | 5 ++ package.json | 1 + tests/fixtures/changelog-entry.json | 31 ++++++++++++ tests/fixtures/updated-changelog-file.json | 12 +++++ .../build-changelog-test.js.snap | 50 ------------------- tests/graphql/build-changelog-test.js | 20 ++++++-- 7 files changed, 67 insertions(+), 54 deletions(-) create mode 100644 tests/fixtures/changelog-entry.json create mode 100644 tests/fixtures/updated-changelog-file.json delete mode 100644 tests/graphql/__snapshots__/build-changelog-test.js.snap diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 502eeafab6..9239972028 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,7 +36,7 @@ jobs: strategy: fail-fast: false matrix: - test-group: [content, meta, rendering, routing, unit, links-and-images] + test-group: [content, meta, rendering, routing, unit, links-and-images, graphql] steps: # Each of these ifs needs to be repeated at each step to make sure the required check still runs # Even if if doesn't do anything diff --git a/package-lock.json b/package-lock.json index fe4a5a67b7..9cea144337 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15892,6 +15892,11 @@ "integrity": "sha1-mDaL6wnfdT9k9m2U5VNql7NqJDA=", "dev": true }, + "mockdate": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mockdate/-/mockdate-3.0.2.tgz", + "integrity": "sha512-ldfYSUW1ocqSHGTK6rrODUiqAFPGAg0xaHqYJ5tvj1hQyFsjuHpuWgWFTZWwDVlzougN/s2/mhDr8r5nY5xDpA==" + }, "morgan": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz", diff --git a/package.json b/package.json index 067dc8fcd4..e1327dd1ed 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "lodash": "^4.17.19", "mini-css-extract-plugin": "^0.9.0", "mkdirp": "^1.0.3", + "mockdate": "^3.0.2", "morgan": "^1.9.1", "node-fetch": "^2.6.1", "platform-utils": "^1.2.0", diff --git a/tests/fixtures/changelog-entry.json b/tests/fixtures/changelog-entry.json new file mode 100644 index 0000000000..900dbe3a3b --- /dev/null +++ b/tests/fixtures/changelog-entry.json @@ -0,0 +1,31 @@ +{ + "previewChanges": [ + { + "changes": [ + "Field `Query.previewField` changed type from `PreviewType` to `PreviewType!`", + "Type for argument `changeTypeArgument` on field 'PreviewType.field1` changed from `Int` to `Float'" + ], + "title": "The [Test preview](/graphql/overview/schema-previews#test-preview) includes these changes:" + } + ], + "schemaChanges": [ + { + "changes": [ + "Field `removedField` was removed from object type `Query`", + "Type for argument `argumentMadeRequired` on field `Query.argumentsField` changed from `Int` to `Int!`", + "Type for argument `argumentMadeOptional` on field `Query.argumentsField` changed from `Int!` to `Int`", + "Argument `removedRequiredArgument: Int!` was removed from field `Query.argumentsField`", + "Argument `removedOptionalArgument: Int!` was removed from field `Query.argumentsField`" + ], + "title": "The GraphQL schema includes these changes:" + } + ], + "upcomingChanges": [ + { + "changes": [ + "On member `Query.stableField`:`stableField` will be removed. **Effective 2021-06-01**." + ], + "title": "The following changes will be made to the schema:" + } + ] +} diff --git a/tests/fixtures/updated-changelog-file.json b/tests/fixtures/updated-changelog-file.json new file mode 100644 index 0000000000..3e8ee60410 --- /dev/null +++ b/tests/fixtures/updated-changelog-file.json @@ -0,0 +1,12 @@ +[ + { + "someStuff": true, + "date": "2020-11-20" + }, + { + "previous_entry": "..." + }, + { + "another_previous_entry": "..." + } +] diff --git a/tests/graphql/__snapshots__/build-changelog-test.js.snap b/tests/graphql/__snapshots__/build-changelog-test.js.snap deleted file mode 100644 index bcf7b6d289..0000000000 --- a/tests/graphql/__snapshots__/build-changelog-test.js.snap +++ /dev/null @@ -1,50 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`creating a changelog from old schema and new schema finds a diff of schema changes, upcoming changes, and preview changes 1`] = ` -Object { - "previewChanges": Array [ - Object { - "changes": Array [ - "Field \`Query.previewField\` changed type from \`PreviewType\` to \`PreviewType!\`", - "Type for argument \`changeTypeArgument\` on field 'PreviewType.field1\` changed from \`Int\` to \`Float'", - ], - "title": "The [Test preview](/graphql/overview/schema-previews#test-preview) includes these changes:", - }, - ], - "schemaChanges": Array [ - Object { - "changes": Array [ - "Field \`removedField\` was removed from object type \`Query\`", - "Type for argument \`argumentMadeRequired\` on field \`Query.argumentsField\` changed from \`Int\` to \`Int!\`", - "Type for argument \`argumentMadeOptional\` on field \`Query.argumentsField\` changed from \`Int!\` to \`Int\`", - "Argument \`removedRequiredArgument: Int!\` was removed from field \`Query.argumentsField\`", - "Argument \`removedOptionalArgument: Int!\` was removed from field \`Query.argumentsField\`", - ], - "title": "The GraphQL schema includes these changes:", - }, - ], - "upcomingChanges": Array [ - Object { - "changes": Array [ - "On member \`Query.stableField\`:\`stableField\` will be removed. **Effective 2021-06-01**.", - ], - "title": "The following changes will be made to the schema:", - }, - ], -} -`; - -exports[`updating the changelog file modifies the entry object and the file on disk 1`] = ` -"[ - { - \\"someStuff\\": true, - \\"date\\": \\"2020-11-23\\" - }, - { - \\"previous_entry\\": \\"...\\" - }, - { - \\"another_previous_entry\\": \\"...\\" - } -]" -`; diff --git a/tests/graphql/build-changelog-test.js b/tests/graphql/build-changelog-test.js index 35342eb6b4..ab2f9cf524 100644 --- a/tests/graphql/build-changelog-test.js +++ b/tests/graphql/build-changelog-test.js @@ -1,8 +1,15 @@ const yaml = require('js-yaml') const { createChangelogEntry, cleanPreviewTitle, previewAnchor, prependDatedEntry } = require('../../script/graphql/build-changelog') const fs = require('fs') +const MockDate = require("mockdate") +const expectedChangelogEntry = require('../fixtures/changelog-entry') +const expectedUpdatedChangelogFile = require('../fixtures/updated-changelog-file') describe('creating a changelog from old schema and new schema', () => { + afterEach(() => { + MockDate.reset() + }) + it('finds a diff of schema changes, upcoming changes, and preview changes', async () => { const oldSchemaString = ` type PreviewType { @@ -68,7 +75,7 @@ upcoming_changes: `).upcoming_changes const entry = await createChangelogEntry(oldSchemaString, newSchemaString, previews, oldUpcomingChanges, newUpcomingChanges) - expect(entry).toMatchSnapshot() + expect(entry).toEqual(expectedChangelogEntry) }) it('returns null when there isnt any difference', async () => { @@ -100,18 +107,25 @@ describe('Preparing preview links', () => { }) describe('updating the changelog file', () => { + afterEach(() => { + MockDate.reset() + }) + it('modifies the entry object and the file on disk', () => { const testTargetPath = 'tests/graphql/example_changelog.json' const previousContents = fs.readFileSync(testTargetPath) const exampleEntry = { someStuff: true } + const expectedDate = "2020-11-20" + MockDate.set(expectedDate) + prependDatedEntry(exampleEntry, testTargetPath) const newContents = fs.readFileSync(testTargetPath, 'utf8') // reset the file: fs.writeFileSync(testTargetPath, previousContents) - const expectedDate = (new Date()).toISOString().split('T')[0] + expect(exampleEntry).toEqual({ someStuff: true, date: expectedDate }) - expect(newContents).toMatchSnapshot() + expect(JSON.parse(newContents)).toEqual(expectedUpdatedChangelogFile) }) })