Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

Test setup #73

Merged
merged 4 commits into from
Mar 11, 2021
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
.env
*.graphql
.cache
*.log
public/
4 changes: 3 additions & 1 deletion plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.js
*.js.map
*.d.ts
README.md
README.md

!jest.config.js
4 changes: 4 additions & 0 deletions plugin/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
11 changes: 8 additions & 3 deletions plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
"version": "1.12.6",
"description": "",
"scripts": {
"watch": "tsc-watch",
"build": "tsc",
"prepublish": "yarn build && cp ../README.md ."
"watch": "tsc-watch --outDir .",
"build": "tsc --outDir .",
"prepublish": "yarn build && cp ../README.md .",
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -22,8 +23,12 @@
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@types/jest": "^26.0.20",
"gatsby-plugin-image": "^1.0.0-next.2",
"jest": "^26.6.3",
"msw": "^0.27.1",
"prettier": "^2.2.1",
"ts-jest": "^26.5.3",
"tsc-watch": "^4.2.9",
"typescript": "^4.2.3"
},
Expand Down
47 changes: 47 additions & 0 deletions plugin/test/sample-test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { graphql } from "msw";
import { SourceNodesArgs } from "gatsby";
import { setupServer } from "msw/node";
import { createOperations } from "../src/operations";

const server = setupServer(
graphql.query("OPERATION_BY_ID", (req, res, ctx) => {
return res(
ctx.data({
node: {
id: req.body?.variables?.id,
status: `COMPLETED`,
},
})
Comment on lines +7 to +14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @wardpeet for this hot tip, this is a really nice DX for mocking GraphQL requests!

);
})
);

beforeAll(() => {
server.listen();
});

afterAll(() => {
server.close();
});

test("Sample test", async () => {
const mock = jest.fn().mockImplementation(() => ({
reporter: {},
}));

const args = mock as jest.Mock<SourceNodesArgs>;

const operations = createOperations(
{
apiKey: "12345",
password: "12345",
storeUrl: "my-shop.shopify.com",
},
args()
);

const resp = await operations.completedOperation(`12345`);

expect(resp.node.status).toEqual(`COMPLETED`);
expect(resp.node.id).toEqual(`12345`);
});
3 changes: 3 additions & 0 deletions plugin/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"include": ["../**/*.ts"]
}
3 changes: 1 addition & 2 deletions plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"include": ["src/**/*.ts", "types"],
"exclude": ["node_modules", "test/**/*.ts"],
"exclude": ["node_modules"],
"compilerOptions": {
"strict": true,
"noUnusedLocals": true,
Expand All @@ -12,7 +12,6 @@
"skipLibCheck": true,
"esModuleInterop": true,
"sourceMap": true,
"outDir": "./",
"target": "es2017",
"declaration": true,
"lib": ["es2017", "dom", "esnext.asynciterable"]
Expand Down
Loading