Skip to content

Commit

Permalink
chore: add test commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jagoral committed Dec 18, 2023
1 parent 1888ead commit 88ae604
Show file tree
Hide file tree
Showing 35 changed files with 239 additions and 210 deletions.
101 changes: 101 additions & 0 deletions .github/workflows/continuous-integration-storefront.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: Continuous Integration (Storefront)
on:
pull_request:
types: ["opened", "edited", "reopened", "synchronize"]

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

defaults:
run:
working-directory: packages/storefront

jobs:
scanning:
name: GitGuardian scan
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0 # fetch all history so multiple commits can be scanned
- name: GitGuardian scan
uses: GitGuardian/[email protected]
with:
args: -v
env:
GITHUB_PUSH_BEFORE_SHA: ${{ github.event.before }}
GITHUB_PUSH_BASE_SHA: ${{ github.event.base }}
GITHUB_PULL_BASE_SHA: ${{ github.event.pull_request.base.sha }}
GITHUB_DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
GITGUARDIAN_API_KEY: ${{ secrets.GITGUARDIAN_API_KEY }}

run-ci:
name: Run CI
runs-on: ubuntu-latest
strategy:
matrix:
node_version: ["18"]
permissions:
contents: read
id-token: write
steps:
- name: Expose github environment as shell variables
env:
SECRETS_CONTEXT: ${{ toJson(secrets) }}
VARS_CONTEXT: ${{ toJson(vars) }}
run: |
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
to_envs() { jq -r "to_entries[] | \"\(.key)<<$EOF\n\(.value)\n$EOF\n\""; }
echo "$SECRETS_CONTEXT" | to_envs >> $GITHUB_ENV
- name: Checkout code 🛎️
uses: actions/checkout@v3

- name: Setup node 🏗️
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node_version }}

- name: Get cache 🗄️
id: cache
uses: actions/cache@v3
with:
path: "**/node_modules"
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}

- run: npm config set //registry.npmjs.org/:_authToken=${{ secrets.NPM_RELEASE_TOKEN }}

- name: Install dependencies
shell: bash
if: steps.cache.outputs.cache-hit != true
run: HUSKY=0 yarn --frozen-lockfile

- name: Detect circular dependencies 🔄
uses: vuestorefront/engineering-toolkit/github-actions/circular-dependencies@main
with:
filesPath: "packages/**/*.{ts,vue}"

- name: Check licenses 🧪
uses: vuestorefront/engineering-toolkit/github-actions/check-licenses@main
with:
projectPath: ${{ github.workspace }}

- name: Build project
run: yarn build

- name: Run tests
run: yarn test

- name: Upload test coverage
uses: actions/upload-artifact@v3
with:
name: coverage-${{ runner.os }}-${{ github.event.pull_request.head.sha }}
path: |
coverage
packages/**/coverage
- name: Lint project
run: yarn lint
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ coverage

# Nuxt generated files
.nuxt
.output

# Next generated files
.next
Expand Down
5 changes: 5 additions & 0 deletions packages/storefront/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"name": "root-storefront",
"private": true,
"scripts": {
"test:next": "cd packages/next && yarn test",
"test:nuxt": "cd packages/nuxt && yarn test",
"test": "yarn test:next && yarn test:nuxt"
},
"workspaces": {
"packages": [
"packages/**/*"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line import/no-unresolved, import/extensions
import { ClientComponentUsingSdk } from "./ClientComponent";

export default function ClientSidePage() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { ReactNode } from "react";
// eslint-disable-next-line import/extensions, import/no-unresolved
import { Providers } from "./providers";

export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body>
<nav>
<ul>
<li>
<a href="/ssr">SSR Page</a>
</li>
<li>
<a href="/csr">CSR Page</a>
</li>
</ul>
</nav>
<Providers>{children}</Providers>
</body>
</html>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default async function Home() {
return (
<main>
<h1>Home Page</h1>
</main>
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createSdkContext } from "../sdk.config";
// eslint-disable-next-line import/no-unresolved
import { createSdkContext } from "@vue-storefront/next/client";
import { getSdk } from "../sdk.config";

export const [SdkProvider, useSdk] = createSdkContext();
export const [SdkProvider, useSdk] = createSdkContext(getSdk() as any);
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
transpilePackages: ["@storefront/shared"],
// With swcMinify set to true, the build will fail because of some transpilation error, probably an issue on Next.js side
swcMinify: true,
};

module.exports = nextConfig;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const options: CreateSdkOptions = {
},
};

export const { getSdk, createSdkContext } = createSdk(
export const { getSdk } = createSdk(
options,
({ buildModule, middlewareUrl, getRequestHeaders }) => ({
example: buildModule(exampleSdkModule, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createSdkContext } from "../sdk.config";
/* eslint-disable import/no-unresolved */
import { createSdkContext } from "@vue-storefront/next/client";
import { getSdk } from "../sdk.config";

export const [SdkProvider, useSdk] = createSdkContext();
export const [SdkProvider, useSdk] = createSdkContext(getSdk());
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { SdkProvider } from "../hooks";
export default function App({ Component, pageProps }: AppProps) {
return (
<SdkProvider>
<nav>
<ul>
<li>
<a href="/ssr">SSR Page</a>
</li>
<li>
<a href="/csr">CSR Page</a>
</li>
</ul>
</nav>
<Component {...pageProps} />
</SdkProvider>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Home() {
return (
<main>
<h1>Home Page</h1>
</main>
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-unresolved */
/* eslint-disable import/no-relative-packages */
import { exampleSdkModule } from "@storefront/shared";
import { CreateSdkOptions, createSdk } from "@vue-storefront/next";
Expand All @@ -8,7 +9,7 @@ const options: CreateSdkOptions = {
},
};

export const { getSdk, createSdkContext } = createSdk(
export const { getSdk } = createSdk(
options,
({ buildModule, middlewareUrl, getRequestHeaders }) => ({
example: buildModule(exampleSdkModule, {
Expand Down
49 changes: 34 additions & 15 deletions packages/storefront/packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,50 @@
"name": "@vue-storefront/next",
"license": "MIT",
"version": "0.0.1",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./client": {
"import": "./dist/client.js",
"require": "./dist/client.js",
"types": "./dist/client.d.ts"
}
},
"typesVersions": {
"*": {
"*": [
"dist/index.d.ts"
],
"client": [
"dist/client.d.ts"
]
}
},
"files": [
"dist"
],
"scripts": {
"build": "tsup src/index.ts --dts-resolve",
"build": "tsup && yarn build:app-router && yarn build:pages-router",
"build:app-router": "cd __tests__/apps/app-router && yarn build",
"build:pages-router": "cd __tests__/apps/pages-router && yarn build",
"dev:app-router": "cd __tests__/apps/app-router && yarn dev",
"dev:pages-router": "cd __tests__/apps/pages-router && yarn dev",
"dev:middleware": "cd ../shared/src/__tests__/middleware && yarn dev",
"lint": "eslint . --ext .ts",
"test:unit": "vitest",
"test:e2e": "cypress run"
"test:app-router": "start-server-and-test dev:middleware localhost:4000/test_integration/success dev:app-router localhost:3000 \"yarn test:e2e\"",
"test:pages-router": "start-server-and-test dev:middleware localhost:4000/test_integration/success dev:pages-router localhost:3000 \"yarn test:e2e\"",
"test": "yarn test:app-router && yarn test:pages-router",
"test:e2e": "cd ../shared/ && yarn test:e2e",
"test:unit": "vitest run"
},
"devDependencies": {
"@types/react": "^18.2.31",
"@types/react-dom": "^18.2.14",
"cypress": "^13.6.1",
"react-dom": "^18.2.0",
"start-server-and-test": "^2.0.3",
"tsup": "8.0.1",
"vitest": "^0.34.6"
},
Expand All @@ -30,13 +57,5 @@
"engines": {
"npm": ">=8.0.0",
"node": ">=18.0.0"
},
"tsup": {
"clean": true,
"target": "es2019",
"format": [
"cjs",
"esm"
]
}
}
3 changes: 3 additions & 0 deletions packages/storefront/packages/next/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use client";

export * from "./sdk/client";
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

"use client";

import { initSDK } from "@vue-storefront/sdk";
import { SDKApi } from "@vue-storefront/sdk";
import Script from "next/script";
import React, { createContext, useContext } from "react";
import { SdkProviderProps } from "../types";
import { SdkProviderProps } from "./types";

export function createSdkContext<TSdk extends ReturnType<typeof initSDK>>(
sdk: TSdk
) {
export function createSdkContext<TSdk extends SDKApi<any>>(sdk: TSdk) {
const SdkContext = createContext<TSdk>(sdk);

function SdkProvider({ children }: SdkProviderProps) {
Expand Down
1 change: 0 additions & 1 deletion packages/storefront/packages/next/src/sdk/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
// eslint-disable-next-line import/extensions, import/no-unresolved
export * from "./SdkContext";
export * from "./resolveDynamicContext";
5 changes: 2 additions & 3 deletions packages/storefront/packages/next/src/sdk/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { buildModule, initSDK } from "@vue-storefront/sdk";
import {
composeMiddlewareUrl,
type CreateSdkOptions,
} from "@storefront/shared";
import { createSdkContext, resolveDynamicContext } from "./helpers";
import { buildModule, initSDK } from "@vue-storefront/sdk";
import { resolveDynamicContext } from "./helpers";
import { Config, CreateSdkReturn, GetSdkContext } from "./types";

export type { CreateSdkOptions } from "@storefront/shared";
Expand Down Expand Up @@ -67,6 +67,5 @@ export function createSdk<TConfig extends Record<string, any>>(

return {
getSdk,
createSdkContext: () => createSdkContext(getSdk()),
};
}
19 changes: 0 additions & 19 deletions packages/storefront/packages/next/src/sdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,4 @@ export interface CreateSdkReturn<TConfig extends Record<string, any>> {
* ```
*/
getSdk: (dynamicContext?: GetSdkContext) => SDKApi<TConfig>;

/**
* Creates a new SDK context. This function is dedicated for the client-side usage.
*
* @example
* Create a new SDK context somewhere in your application. It may be the `hooks/sdk.ts` file.
*
* ```tsx
* import { createSdkContext } from "../../sdk.config";
*
* export const [SdkProvider, useSdk] = createSdkContext();
* ```
* Then use the `SdkProvider` in the root component of your application.
* For Pages Router it would be the `pages/_app.tsx` file,
* and for the App Router it would be the `app/layout.tsx` file.
* Finally you can use the `useSdk` in any client component of your application.
* @returns [SdkProvider, useSdk] - The SDK provider and the `useSdk` hook.
*/
createSdkContext: () => CreateSdkContextReturn<TConfig>;
}
11 changes: 11 additions & 0 deletions packages/storefront/packages/next/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: {
index: "src/index.ts",
client: "src/client.ts",
},
target: "es2015",
format: ["cjs", "esm"],
dts: true,
});
10 changes: 10 additions & 0 deletions packages/storefront/packages/nuxt/__tests__/app/app.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<template>
<div>
<nav>
<ul>
<li>
<a href="/ssr">SSR Page</a>
</li>
<li>
<a href="/csr">CSR Page</a>
</li>
</ul>
</nav>
<NuxtPage />
</div>
</template>
Loading

0 comments on commit 88ae604

Please sign in to comment.