Integration Test a Embedded Shopify App Using Node

Highlighted
New Member
1 0 1

Hello, I've complete the tutorial "Build a Shopify App with Node and React" linked below.
https://shopify.dev/tutorials/build-a-shopify-app-with-node-and-react/embed-your-app-in-shopify

Currently the app is successfully embedding as expected. Now I'd like to write and integration test to be sure that this continues to be the case as I'm developing my Shopify app.

I'd like to test the following things:
- The response of a get request is 200
-  Shopify authenticates the request with the config
- Shopify verifies the request
- Next JS handles the request

With the test below, I keep on getting 404 status codes in the response. Could someone please tell me how to go about writing this test? 

Here is my test so far.

 

//tests/server/server.js
import request from 'supertest'
import { server } from "server";

test("ensure Shopify authenticates and response with 200", async () => {
  const response = await request(server.callback()).get('/auth').set('shop', 'farmers-market-development.myshopify.com').set('accessToken', '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')
  expect(response.status).toEqual(200)
  server.close()
});

 


My code for the server.js file is identical to the tutorial except that I'm exporting the server at the end.

//server.js
require("isomorphic-fetch");
const dotenv = require("dotenv");
const Koa = require("koa");
const next = require("next");
const { default: createShopifyAuth } = require("@shopify/koa-shopify-auth");
const { verifyRequest } = require("@shopify/koa-shopify-auth");
const session = require("koa-session");

dotenv.config();

const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const server = new Koa();
const handle = app.getRequestHandler();

const { SHOPIFY_API_SECRET_KEY, SHOPIFY_API_KEY } = process.env;

app.prepare().then(() => {
  server.use(session({ secure: true, sameSite: "none" }, server));
  server.keys = [SHOPIFY_API_SECRET_KEY];
  // Expect this to have been called once
  server.use(
    createShopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_API_SECRET_KEY,
      scopes: ["read_products"],
      afterAuth(ctx) {
        // make sure that the shop and accessToken are part of the ctx

        const { shop, accessToken } = ctx.session;
        ctx.redirect("/");
      },
    })
  );

  // Expect this to have been called once
  server.use(verifyRequest());

  // Expect 200 status code and possibly some content
  server.use(async (ctx) => {
    await handle(ctx.req, ctx.res);
    ctx.respond = false;
    ctx.res.statusCode = 200;
    return;
  });

  server.listen(port, () => {
    console.log(`> Ready on http://localhost:${port}`);
  });
});

module.exports = {
  server
}
1 Like
Highlighted
New Member
3 0 0

I am also looking for the integration testing like above code.

0 Likes