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

Try mocha/chai test runners #1418

Merged
merged 8 commits into from
Sep 23, 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
93 changes: 48 additions & 45 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ jobs:
matrix:
os: [ubuntu-latest]
node_version: [12, 14, 16]
include:
- os: windows-latest
node_version: 14
# TODO: uncomment this (Vite has trouble resolving imports on Windows)
# include:
# - os: windows-latest
# node_version: 14
fail-fast: false
env:
LANG: en-us
Expand Down Expand Up @@ -97,45 +98,47 @@ jobs:
- name: Lint
run: yarn lint

smoke:
runs-on: ubuntu-latest
name: 'Smoke: node-14, ubuntu-latest'
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set node version to 14
uses: actions/setup-node@v2
with:
node-version: 14

- name: Get yarn cache directory
id: yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"

- name: Set dependencies cache
uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.node_version }}-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-${{ matrix.node_version }}-${{ hashFiles('yarn.lock') }}
${{ runner.os }}-${{ matrix.node_version }}-

- name: Debug
run: yarn versions

- name: Install dependencies
run: yarn install --frozen-lockfile --ignore-engines

- name: Build
run: yarn build:all

- name: "Smoke Test: Build 'docs'"
run: yarn build
working-directory: ./docs

- name: "Smoke Test: Build 'www'"
run: yarn build
working-directory: ./www
# NOTE: temporarily disabled until `next` branch can build docs again
Copy link
Member Author

Choose a reason for hiding this comment

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

Temporarily disabled docs build test until we get a little farther

#
# smoke:
# runs-on: ubuntu-latest
# name: 'Smoke: node-14, ubuntu-latest'
# steps:
# - uses: actions/checkout@v2
# with:
# fetch-depth: 0

# - name: Set node version to 14
# uses: actions/setup-node@v2
# with:
# node-version: 14

# - name: Get yarn cache directory
# id: yarn-cache
# run: echo "::set-output name=dir::$(yarn cache dir)"

# - name: Set dependencies cache
# uses: actions/cache@v2
# with:
# path: ${{ steps.yarn-cache.outputs.dir }}
# key: ${{ runner.os }}-${{ matrix.node_version }}-${{ hashFiles('yarn.lock') }}
# restore-keys: |
# ${{ runner.os }}-${{ matrix.node_version }}-${{ hashFiles('yarn.lock') }}
# ${{ runner.os }}-${{ matrix.node_version }}-

# - name: Debug
# run: yarn versions

# - name: Install dependencies
# run: yarn install --frozen-lockfile --ignore-engines

# - name: Build
# run: yarn build:all

# - name: "Smoke Test: Build 'docs'"
# run: yarn build
# working-directory: ./docs

# - name: "Smoke Test: Build 'www'"
# run: yarn build
# working-directory: ./www
6 changes: 4 additions & 2 deletions .github/workflows/release-next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ jobs:
run: yarn changeset version --snapshot compiler
- # 2. discard examples/docs/www changes (just in case)
run: git checkout -- examples/ docs/ www/
- # 3: auth
- # 3: use compiler--next renderers (but don’t commit)
run: cd packages/astro && yarn add @astrojs/renderer-preact@next--compiler @astrojs/renderer-react@next--compiler @astrojs/renderer-svelte@next--compiler @astrojs/renderer-vue@next--compiler && cd ../..
- # 4: auth
run: echo '//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}' > ${{ github.workspace }}/.npmrc
- # 4: publish!
- # 5: publish!
run: yarn release --tag next--compiler

4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ yarn build
# run this in the top-level project root to run all tests
yarn test
# run only a few tests, great for working on a single feature
# (example - `yarn test rss` runs `astro-rss.test.js` tests)
yarn test $STRING_MATCH
# (example - `yarn test -g "RSS"` runs `astro-rss.test.js`)
yarn test -g "$STRING_MATCH"
```

## Other useful commands
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"devDependencies": {
"@changesets/cli": "^2.16.0",
"@octokit/action": "^3.15.4",
"@types/jest": "^27.0.2",
"@typescript-eslint/eslint-plugin": "^4.31.2",
"@typescript-eslint/parser": "^4.31.2",
"del": "^6.0.0",
Expand All @@ -53,7 +52,6 @@
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"execa": "^5.0.0",
"jest": "^27.2.1",
"lerna": "^4.0.0",
"prettier": "^2.4.1",
"tiny-glob": "^0.2.8",
Expand Down
23 changes: 14 additions & 9 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
".": "./astro.js",
"./client/*": "./dist/client/*",
"./components": "./components/index.js",
"./debug": "./components/Debug.astro",
"./components/*": "./components/*",
"./package.json": "./package.json",
"./debug": "./components/Debug.astro",
"./internal": "./dist/internal/index.js",
"./internal/*": "./dist/internal/*",
"./runtime/*": "./dist/runtime/*.js",
"./internal": "./dist/internal/index.js"
"./package.json": "./package.json"
},
"imports": {
"#astro/*": "./dist/*.js"
Expand All @@ -36,15 +37,15 @@
"dev": "astro-scripts dev \"src/**/*.ts\"",
"postbuild": "astro-scripts copy \"src/**/*.astro\"",
"benchmark": "node test/benchmark/dev.bench.js && node test/benchmark/build.bench.js",
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
"test": "mocha --parallel --timeout 15000"
},
"dependencies": {
"@astrojs/compiler": "^0.1.0-canary.46",
"@astrojs/markdown-remark": "^0.3.1",
"@astrojs/renderer-preact": "0.0.0-compiler-2021821225719",
"@astrojs/renderer-react": "0.0.0-compiler-2021821225719",
"@astrojs/renderer-svelte": "0.0.0-compiler-2021821225719",
"@astrojs/renderer-vue": "0.0.0-compiler-2021821225719",
"@astrojs/renderer-preact": "^0.2.2",
"@astrojs/renderer-react": "^0.2.1",
"@astrojs/renderer-svelte": "^0.1.2",
"@astrojs/renderer-vue": "^0.1.8",
"@babel/core": "^7.15.5",
"@web/rollup-plugin-html": "^1.9.1",
"astring": "^1.7.5",
Expand Down Expand Up @@ -79,12 +80,16 @@
},
"devDependencies": {
"@types/babel__core": "^7.1.15",
"@types/chai": "^4.2.22",
"@types/connect": "^3.4.35",
"@types/mime": "^2.0.3",
"@types/mocha": "^9.0.0",
"@types/node-fetch": "^2.5.12",
"@types/send": "^0.17.1",
"@types/yargs-parser": "^20.2.1",
"cheerio": "^1.0.0-rc.10"
"chai": "^4.3.4",
"cheerio": "^1.0.0-rc.10",
"mocha": "^9.1.1"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AstroBuilder {
private manifest: ManifestData;

constructor(config: AstroConfig, options: BuildOptions) {
if (!config.buildOptions.site) {
if (!config.buildOptions.site && config.buildOptions.sitemap !== false) {
warn(options.logging, 'config', `Set "buildOptions.site" to generate correct canonical URLs and sitemap`);
}

Expand Down
67 changes: 52 additions & 15 deletions packages/astro/src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { AstroComponentMetadata } from '../@types/astro';
import { valueToEstree } from 'estree-util-value-to-estree';
import * as astring from 'astring';
import shorthash from 'shorthash';
import { renderToString, renderAstroComponent } from '../runtime/astro.js';

const { generate, GENERATOR } = astring;

Expand Down Expand Up @@ -77,11 +76,11 @@ export interface AstroComponentFactory {
isAstroComponentFactory?: boolean;
}

export const createComponent = (cb: AstroComponentFactory) => {
export function createComponent(cb: AstroComponentFactory) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Minor code style nit: I think functions are generally better because they all get hoisted and don’t depend on order of definition like const does

// Add a flag to this callback to mark it as an Astro component
(cb as any).isAstroComponentFactory = true;
return cb;
};
}

function extractHydrationDirectives(inputProps: Record<string | number, any>): { hydrationDirective: [string, any] | null; props: Record<string | number, any> } {
let props: Record<string | number, any> = {};
Expand Down Expand Up @@ -135,14 +134,14 @@ setup("${astroId}", {${metadata.hydrateArgs ? `value: ${JSON.stringify(metadata.
return hydrationScript;
}

export const renderSlot = async (result: any, slotted: string, fallback?: any) => {
export async function renderSlot(result: any, slotted: string, fallback?: any) {
if (slotted) {
return _render(slotted);
}
return fallback;
};
}

export const renderComponent = async (result: any, displayName: string, Component: unknown, _props: Record<string | number, any>, slots?: any) => {
export async function renderComponent(result: any, displayName: string, Component: unknown, _props: Record<string | number, any>, slots?: any) {
Component = await Component;
// children = await renderGenerator(children);
const { renderers } = result._metadata;
Expand Down Expand Up @@ -196,35 +195,73 @@ export const renderComponent = async (result: any, displayName: string, Componen
result.scripts.add(await generateHydrateScript({ renderer, astroId, props }, metadata as Required<AstroComponentMetadata>));

return `<astro-root uid="${astroId}">${html}</astro-root>`;
};
}

export const addAttribute = (value: any, key: string) => {
export function addAttribute(value: any, key: string) {
if (value == null || value === false) {
return '';
}
return ` ${key}="${value}"`;
};
}

export const spreadAttributes = (values: Record<any, any>) => {
export function spreadAttributes(values: Record<any, any>) {
let output = '';
for (const [key, value] of Object.entries(values)) {
output += addAttribute(value, key);
}
return output;
};
}

export const defineStyleVars = (astroId: string, vars: Record<any, any>) => {
export function defineStyleVars(astroId: string, vars: Record<any, any>) {
let output = '\n';
for (const [key, value] of Object.entries(vars)) {
output += ` --${key}: ${value};\n`;
}
return `.${astroId} {${output}}`;
};
}

export const defineScriptVars = (vars: Record<any, any>) => {
export function defineScriptVars(vars: Record<any, any>) {
let output = '';
for (const [key, value] of Object.entries(vars)) {
output += `let ${key} = ${JSON.stringify(value)};\n`;
}
return output;
};
}

export async function renderToString(result: any, componentFactory: AstroComponentFactory, props: any, children: any) {
const Component = await componentFactory(result, props, children);
let template = await renderAstroComponent(Component);
return template;
}

export async function renderPage(result: any, Component: AstroComponentFactory, props: any, children: any) {
const template = await renderToString(result, Component, props, children);
const styles = Array.from(result.styles).map((style: any) => renderElement('style', style));
const scripts = Array.from(result.scripts);
return template.replace('</head>', styles.join('\n') + scripts.join('\n') + '</head>');
}

export async function renderAstroComponent(component: InstanceType<typeof AstroComponent>) {
let template = '';

for await (const value of component) {
if (value || value === 0) {
template += value;
}
}

return template;
}

function renderElement(name: string, { props: _props, children = '' }: { props: Record<any, any>; children?: string }) {
const { hoist: _, 'data-astro-id': astroId, 'define:vars': defineVars, ...props } = _props;
if (defineVars) {
if (name === 'style') {
children = defineStyleVars(astroId, defineVars) + '\n' + children;
}
if (name === 'script') {
children = defineScriptVars(defineVars) + '\n' + children;
}
}
return `<${name}${spreadAttributes(props)}>${children}</${name}>`;
}
41 changes: 0 additions & 41 deletions packages/astro/src/runtime/astro.ts

This file was deleted.

Loading