Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(publish): improve e2e tests #3580

Merged
merged 19 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
dc476e9
chore: remove unused `cypress.json` file
namjul Sep 23, 2022
d410529
chore: rename playwright test folder to `e2e`
namjul Sep 23, 2022
0d226aa
chore(publish): add `NODE_ENV` to `env` handler
namjul Sep 23, 2022
b27bd19
chore(publish, test): correct ts types
namjul Sep 26, 2022
8998df0
chore(publish): split env into client and schema
namjul Sep 26, 2022
a0baf47
chore(publish): test to make sure NEXT_PUBLIC_ASSET_PREFIX env is used
namjul Sep 26, 2022
22f70a7
chore(publish): correct test description
namjul Sep 26, 2022
0263076
chore(publish): update playwright to use new `toHaveScreenshot` api
namjul Sep 26, 2022
8cbfa94
chore(publish): allow passing arguments to `cli:test:template:docker`…
namjul Sep 26, 2022
c2c95e8
chore(publish): disable e2e test with NEXT_PUBLIC_ASSET_PREFIX set
namjul Sep 26, 2022
0ca0c5d
chore(publish): revert changes
namjul Sep 26, 2022
4460801
chore(publish): update visual snapshots
namjul Sep 26, 2022
5e6517f
chore(publish): revert logic to allow NODE_ENV=test
namjul Sep 27, 2022
54cf881
chore(publish): consider `NEXT_PUBLIC_ASSET_PREFIX` when running e2e …
namjul Sep 28, 2022
9d8d958
chore: adjust command to forward arguments intuitively
namjul Sep 29, 2022
6e3d250
chore(publish): update e2e VRT snapshots
namjul Sep 29, 2022
9e892e5
chore(publish): add e2e test for `NEXT_PUBLIC_ASSET_PREFIX`
namjul Sep 29, 2022
a073c04
chore(publish): fix react key warning
namjul Sep 29, 2022
9d8295b
chore(publish, e2e): rephrase test case description
namjul Sep 30, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/nextjs-template/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_ASSET_PREFIX=/my_asset_folder
3 changes: 0 additions & 3 deletions packages/nextjs-template/cypress.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,57 @@ import test from "./next-fixture";
import { expect } from "@playwright/test";

test.describe("GIVEN default viewport", () => {
test("THEN should displayed heading anchor pleasantly ", async ({
test("THEN should display heading anchor pleasantly ", async ({
page,
port,
}) => {
await page.goto(`http://localhost:${port}/notes/FSi3bKWQeQXYTjE1PoTB0/`);

const case1 = page.locator("#start-code-end");
await case1.hover();

expect(await case1.screenshot()).toMatchSnapshot([
"heading-anchor",
"start-code-end.png",
]);

const case2 = page.locator("#start---end");
await case2.hover();

expect(await case2.screenshot()).toMatchSnapshot([
"heading-anchor",
"start---end.png",
]);

const case3 = page.locator("#start-_-end");
await case3.hover();

expect(await case3.screenshot()).toMatchSnapshot([
"heading-anchor",
"start-_-end.png",
]);

const case4 = page.locator("#start-test-end");
await case4.hover();

expect(await case4.screenshot()).toMatchSnapshot([
"heading-anchor",
"start-test-end.png",
]);

const case5 = page.locator("#start-exampleusername-private-end");
await case5.hover();

expect(await case5.screenshot()).toMatchSnapshot([
"heading-anchor",
"start-exampleusername-private-end.png",
]);
});

test.describe("AND having NEXT_PUBLIC_ASSET_PREFIX set", () => {
test("THEN should render path to favicon", async ({ page, port }) => {
await page.goto(`http://localhost:${port}/`);
const locator = page.locator(
`link[href*='${process.env.NEXT_PUBLIC_ASSET_PREFIX}'][rel='icon']`
);
expect(await locator.count()).toBe(1);
});
});
});

test.describe("GIVEN mobile viewport", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {};
/* eslint-disable no-console */

const path = require("path");
const cli = require("next/dist/cli/next-build");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ import { AddressInfo } from "net";
*/

// Extend base test with our fixtures.
const test = base.extend<{
port: string;
}>({
const test = base.extend<
{},
{
port: string;
}
>({
port: [
// eslint-disable-next-line no-empty-pattern
async ({}, use) => {
Expand Down Expand Up @@ -49,7 +52,6 @@ const test = base.extend<{
await use(port);
},
{
//@ts-ignore
scope: "worker",
},
],
Expand Down
13 changes: 13 additions & 0 deletions packages/nextjs-template/env/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @ts-check
const { parse } = require("@dendronhq/common-all");
const { clientEnv, clientSchema } = require("./schema");

const _clientEnv = parse(clientSchema, clientEnv);

if (_clientEnv.error) {
throw _clientEnv.error;
}

module.exports = {
env: _clientEnv.data
};
68 changes: 68 additions & 0 deletions packages/nextjs-template/env/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// @ts-check
const { z } = require("zod");
const path = require("path");
const { FRONTEND_CONSTANTS } = require("@dendronhq/common-frontend");

/**
* Here we specify server-side environment variables
* This way you can ensure the app isn't built with invalid env vars.
*/
const serverSchema = z
.object({
DATA_DIR: z
.string()
.default(path.join(__dirname, "..", FRONTEND_CONSTANTS.DEFAULT_DATA_DIR)),
PUBLIC_DIR: z.string().default(path.join(__dirname, "..", "public")),
NODE_ENV: z.enum(["development", "test", "production"]),
})
.superRefine((value, ctx) => {
/**
* Validate that server-side environment variables are not exposed to the client.
*/
Object.keys(value).forEach((key) => {
if (key.startsWith("NEXT_PUBLIC_")) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `❌ You are exposing a server-side env-variable: ${key}`,
});
}
});
});

/**
* Here we specify client-side environment variables.
* This way you can ensure the app isn't built with invalid env vars.
* To expose them to the client, prefix them with `NEXT_PUBLIC_`.
*/
const clientSchema = z
.object({
NEXT_PUBLIC_ASSET_PREFIX: z.string().optional(),
})
.superRefine((value, ctx) => {
/**
* Validate that client-side environment variables are exposed to the client.
*/
Object.keys(value).forEach((key) => {
if (!key.startsWith("NEXT_PUBLIC_")) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `❌ Invalid public environment variable name: ${key}. It must begin with 'NEXT_PUBLIC_'`,
});
}
});
});

/**
* You can't destruct `process.env` as a regular object, so you have to do
* it manually here. This is because Next.js evaluates this at build time,
* and only used environment variables are included in the build.
*/
const clientEnv = {
NEXT_PUBLIC_ASSET_PREFIX: process.env.NEXT_PUBLIC_ASSET_PREFIX,
};

module.exports = {
serverSchema,
clientSchema,
clientEnv,
};
21 changes: 21 additions & 0 deletions packages/nextjs-template/env/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @ts-check
const { parse } = require("@dendronhq/common-all");
const { serverSchema, } = require("./schema");
const { env: clientEnv } = require("./client")

/**
* This file is included in `/next.config.js` which ensures the app isn't built with invalid env vars.
*/

const serverEnv = parse(serverSchema, process.env);

if (serverEnv.error) {
throw serverEnv.error;
}

module.exports = {
env: {
...serverEnv.data,
...clientEnv
}
};
2 changes: 1 addition & 1 deletion packages/nextjs-template/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { env } = require('./utils/env')
const { env } = require('./env/server')

// throws if validation fails
require('./utils/validation')
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"build:sitemap": "next-sitemap --config sitemap.js",
"clean": "yarn rimraf public data out",
"rimraf": "npx rimraf",
"test": "playwright test",
"test:skipbuild": "SKIP_BUILD=1 playwright test"
"test": "NODE_ENV=test playwright test",
"test:skipbuild": "SKIP_BUILD=1 yarn test"
},
"overrides": {
"trim": "0.0.3",
Expand Down
6 changes: 5 additions & 1 deletion packages/nextjs-template/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import path from "path";
import { PlaywrightTestConfig, devices } from "@playwright/test";

const testDir = path.join(__dirname, "e2e");
namjul marked this conversation as resolved.
Show resolved Hide resolved

const config: PlaywrightTestConfig = {
globalSetup: require.resolve("./tests/global-setup"),
testDir,
globalSetup: require.resolve(path.join(testDir, "global-setup")),
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
// Limit the number of workers on CI, use default locally
Expand Down
37 changes: 0 additions & 37 deletions packages/nextjs-template/utils/env.js

This file was deleted.

9 changes: 4 additions & 5 deletions packages/nextjs-template/utils/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import {
NoteProps,
} from "@dendronhq/common-all";
import _ from "lodash";
import { env } from "../env/client";

export function getNoteUrl(opts: { note: NoteProps; noteIndex: NoteProps }) {
const { note, noteIndex } = opts;
return note.id === noteIndex.id ? "/" : `/notes/${note.id}`;
}

export function getAssetUrl(url: string) {
const out =
process.env.NODE_ENV === "production" &&
process.env.NEXT_PUBLIC_ASSET_PREFIX
? process.env.NEXT_PUBLIC_ASSET_PREFIX + url
: url;
namjul marked this conversation as resolved.
Show resolved Hide resolved
const out = env.NEXT_PUBLIC_ASSET_PREFIX
? env.NEXT_PUBLIC_ASSET_PREFIX + url
: url;
return out;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs-template/utils/validation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs-extra");
const path = require("path");
const { parse, treeMenuSchema } = require("@dendronhq/common-all");
const { env } = require('./env')
const { env } = require('../env/server')

const dataDir = env.DATA_DIR
if (!dataDir) {
Expand Down