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 (internal): convert builder and request tests to use diagnostic+runner #9006

Merged
merged 1 commit into from
Oct 17, 2023
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
21 changes: 20 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ module.exports = {
},
overrides: [
{
files: ['tests/ember-data__json-api/**', 'tests/ember-data__graph/**', 'tests/ember-data__request/**'],
files: [
'tests/ember-data__json-api/**',
'tests/ember-data__graph/**',
'tests/ember-data__request/**',
'tests/builders/**',
],
rules: {
'qunit/no-assert-equal': 'off',
'qunit/no-assert-logical-expression': 'off',
Expand Down Expand Up @@ -272,6 +277,20 @@ module.exports = {
],
},

// modern node files
{
files: ['tests/*/diagnostic.js'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2022,
},
env: {
browser: false,
node: true,
es6: true,
},
},

// node files
{
files: [
Expand Down
2 changes: 1 addition & 1 deletion packages/active-record/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const options = findRecord('ember-developer', '1', { include: ['pets', 'friends'
=> {
url: 'https://api.example.com/v1/ember_developers/1?include=friends,pets',
method: 'GET',
headers: <Headers>, // 'Content-Type': 'application/json; charset=utf-8'
headers: <Headers>, // 'Content-Type': 'application/json;charset=utf-8'
op: 'findRecord';
records: [{ type: 'ember-developer', id: '1' }]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function findRecord(

const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/json; charset=utf-8');
headers.append('Accept', 'application/json;charset=utf-8');

return {
url: options.include?.length
Expand Down
2 changes: 1 addition & 1 deletion packages/active-record/src/-private/builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function query(

const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/json; charset=utf-8');
headers.append('Accept', 'application/json;charset=utf-8');

return {
url: `${url}?${buildQueryParams(query, options.urlParamsSettings)}`,
Expand Down
6 changes: 3 additions & 3 deletions packages/active-record/src/-private/builders/save-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function deleteRecord(record: unknown, options: ConstrainedRequestOptions

const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/json; charset=utf-8');
headers.append('Accept', 'application/json;charset=utf-8');

return {
url,
Expand Down Expand Up @@ -160,7 +160,7 @@ export function createRecord(record: unknown, options: ConstrainedRequestOptions

const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/json; charset=utf-8');
headers.append('Accept', 'application/json;charset=utf-8');

return {
url,
Expand Down Expand Up @@ -236,7 +236,7 @@ export function updateRecord(

const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/json; charset=utf-8');
headers.append('Accept', 'application/json;charset=utf-8');

return {
url,
Expand Down
2 changes: 1 addition & 1 deletion packages/active-record/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const options = findRecord('ember-developer', '1', { include: ['pets', 'friends'
=> {
url: 'https://api.example.com/v1/ember_developers/1?include=friends,pets',
method: 'GET',
headers: <Headers>, // 'Content-Type': 'application/json; charset=utf-8'
headers: <Headers>, // 'Content-Type': 'application/json;charset=utf-8'
op: 'findRecord';
records: [{ type: 'ember-developer', id: '1' }]
}
Expand Down
18 changes: 18 additions & 0 deletions packages/diagnostic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ or you can add your own.
- [Using the DOM Reporter](#using-the-domreporter)
- [Concurrency](#concurrency)
- [Using The Launcher](#using-the-launcher)
- [Adding A Sidecar](#adding-a-sidecar)
- [🔜 Parallelism](#parallelism)
- [🔜 Randomization](#randomization)
- [Why Is It Fast?](#why-is-it-fast)
Expand Down Expand Up @@ -302,6 +303,23 @@ And update any necessary scripts in `package.json`

---

### Adding A Sidecar

Diagnostic's launcher supports running additional services alongside your test suite
when they are necessary for your tests to run correctly. For instance, you may want
to start a local API instance, http mock service, or a build process.

#### Use with @warp-drive/holodeck

@warp-drive/holodeck is an http mock service for test suites. We can start and stop
the holodeck server along side our test server with an easy integration.

```ts

```

---

### Parallelism

[Coming Soon]
Expand Down
7 changes: 6 additions & 1 deletion packages/diagnostic/server/bun/socket-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sinceStart } from "../utils/time.js";
export function buildHandler(config, state) {
return {
perMessageDeflate: true,
message(ws, message) {
async message(ws, message) {
const msg = JSON.parse(message);
msg.launcher = state.browsers.get(msg.browserId).launcher;
info(`${chalk.green('➡')} [${chalk.cyan(msg.browserId)}/${chalk.cyan(msg.windowId)}] ${chalk.green(msg.name)}`);
Expand Down Expand Up @@ -39,6 +39,11 @@ export function buildHandler(config, state) {
browser.proc.unref();
});
state.server.stop();
if (config.cleanup) {
debug(`Running configured cleanup hook`);
await config.cleanup();
debug(`Configured cleanup hook completed`);
}
process.exit(exitCode);
}

Expand Down
25 changes: 14 additions & 11 deletions packages/diagnostic/server/default-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,26 @@ const SUITE_TIMEOUT = process.env.SUITE_TIMEOUT
? Number(process.env.SUITE_TIMEOUT) - SUITE_TIMEOUT_BUFFER
: DEFAULT_SUITE_TIMEOUT;

export default async function launchDefault() {
export default async function launchDefault(overrides = {}) {
await launch({
entry: `./dist-test/tests/index.html?${TEST_PAGE_FLAGS.join('&')}`,
assets: './dist-test',
parallel,
parallelMode: 'window', // 'tab' | 'browser' | 'window'
entry: overrides.entry ?? `./dist-test/tests/index.html?${TEST_PAGE_FLAGS.join('&')}`,
assets: overrides.assets ?? './dist-test',
parallel: overrides.parallel ?? parallel,
parallelMode: overrides.parallelMode ?? 'window', // 'tab' | 'browser' | 'window'

reporter: new DefaultReporter({
reporter: overrides.reporter ?? new DefaultReporter({
mode: process.env.DIAGNOSTIC_REPORTER_MODE || 'dot', // 'dot' | 'compact' | 'verbose'
}),

suiteTimeout: SUITE_TIMEOUT,
browserDisconnectTimeout: 15,
browserStartTimeout: 15,
socketHeartbeatTimeout: 15,
suiteTimeout: overrides.suiteTimeout ?? SUITE_TIMEOUT,
browserDisconnectTimeout: overrides.browserDisconnectTimeout ?? 15,
browserStartTimeout: overrides.browserStartTimeout ?? 15,
socketHeartbeatTimeout: overrides.socketHeartbeatTimeout ?? 15,

launchers: {
setup: overrides.setup ?? (() => {}),
cleanup: overrides.cleanup ?? (() => {}),

launchers: overrides.launchers ?? {
[BROWSER_TAG]: {
command: browser,
args: recommendedArgs(BROWSER_TAG),
Expand Down
12 changes: 11 additions & 1 deletion packages/diagnostic/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const isBun = typeof Bun !== 'undefined';
export default async function launch(config) {
if (isBun) {
debug(`Bun detected, using Bun.serve()`);
if (config.setup) {
debug(`Running configured setup hook`);
await config.setup();
debug(`Configured setup hook completed`);
}
const { checkPort } = await import('./bun/port.js');
const hostname = config.hostname ?? 'localhost';
const protocol = config.protocol ?? 'http';
Expand Down Expand Up @@ -65,9 +70,14 @@ export default async function launch(config) {
await launchBrowsers(config, state);
} catch (e) {
error(`Error: ${e?.message ?? e}`);
if (config.cleanup) {
debug(`Running configured cleanup hook`);
await config.cleanup();
debug(`Configured cleanup hook completed`);
}
throw e;
}
} else {
throw new Error(`Holodeck is not supported in this environment.`);
throw new Error(`Diagnostic is not supported in this environment.`);
}
}
24 changes: 21 additions & 3 deletions packages/diagnostic/server/reporters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,11 @@ export default class CustomDotReporter {
result.items.forEach((diagnostic) => {
this.write(`\t\t${diagnostic.passed ? chalk.green('✅ Pass') : chalk.red('💥 Fail')} ${diagnostic.message}\n`);

if ('expected' in diagnostic && 'actual' in diagnostic) {
this.write(`\n\t\texpected: ${diagnostic.expected}\n\t\tactual: ${diagnostic.actual}\n`);
if (!diagnostic.passed && 'expected' in diagnostic && 'actual' in diagnostic) {
this.write(`\n\t\texpected: ${printValue(diagnostic.expected, 3)}\n\t\tactual: ${printValue(diagnostic.actual, 3)}\n`);
}

if (diagnostic.stack) {
if (!diagnostic.passed && diagnostic.stack) {
this.write(`\n${indent(diagnostic.stack)}\n`);
}
});
Expand Down Expand Up @@ -498,3 +498,21 @@ export default class CustomDotReporter {
function remove(filePath) {
fs.writeFileSync(filePath, '', { encoding: 'utf-8' });
}

function printValue(value, tabs = 0) {
if (typeof value === 'string') {
return value;
} else if (typeof value === 'number') {
return value;
} else if (typeof value === 'boolean') {
return String(value);
} else if (value === null) {
return 'null';
} else if (value === undefined) {
return 'undefined';
} else if (Array.isArray(value)) {
return indent(`[\n ${value.map((v) => printValue(v, tabs + 1)).join(',\n ')}\n]`, tabs);
} else if (typeof value === 'object') {
return JSON.stringify(value, null, tabs * 4);
}
}
3 changes: 3 additions & 0 deletions packages/model/src/-private/model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class Model extends EmberObject {
rollbackAttributes(): void;
changedAttributes(): Record<string, [unknown, unknown]>;
[key: string]: unknown;
isSaving: boolean;
isNew: boolean;
isDeleted: boolean;
hasDirtyAttributes: boolean;
deleteRecord(): void;
unloadRecord(): void;
serialize(): Record<string, unknown>;
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const options = findRecord('ember-developer', '1', { include: ['pets', 'friends'
=> {
url: 'https://api.example.com/v1/emberDevelopers/1?include=friends,pets',
method: 'GET',
headers: <Headers>, // 'Content-Type': 'application/json; charset=utf-8'
headers: <Headers>, // 'Content-Type': 'application/json;charset=utf-8'
op: 'findRecord';
records: [{ type: 'ember-developer', id: '1' }]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/-private/builders/find-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function findRecord(

const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/json; charset=utf-8');
headers.append('Accept', 'application/json;charset=utf-8');

return {
url: options.include?.length
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/-private/builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function query(

const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/json; charset=utf-8');
headers.append('Accept', 'application/json;charset=utf-8');

return {
url: `${url}?${buildQueryParams(query, options.urlParamsSettings)}`,
Expand Down
6 changes: 3 additions & 3 deletions packages/rest/src/-private/builders/save-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function deleteRecord(record: unknown, options: ConstrainedRequestOptions

const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/json; charset=utf-8');
headers.append('Accept', 'application/json;charset=utf-8');

return {
url,
Expand Down Expand Up @@ -160,7 +160,7 @@ export function createRecord(record: unknown, options: ConstrainedRequestOptions

const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/json; charset=utf-8');
headers.append('Accept', 'application/json;charset=utf-8');

return {
url,
Expand Down Expand Up @@ -236,7 +236,7 @@ export function updateRecord(

const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/json; charset=utf-8');
headers.append('Accept', 'application/json;charset=utf-8');

return {
url,
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const options = findRecord('ember-developer', '1', { include: ['pets', 'friends'
=> {
url: 'https://api.example.com/v1/emberDevelopers/1?include=friends,pets',
method: 'GET',
headers: <Headers>, // 'Content-Type': 'application/json; charset=utf-8'
headers: <Headers>, // 'Content-Type': 'application/json;charset=utf-8'
op: 'findRecord';
records: [{ type: 'ember-developer', id: '1' }]
}
Expand Down
20 changes: 8 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/builders/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Provides testing for the Request and URL Building Utils

- @ember-data/active-record/request
- @ember-data/json-api/request
- @ember-data/rest/request
- @ember-data/request-utils
3 changes: 3 additions & 0 deletions tests/builders/diagnostic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import launch from '@warp-drive/diagnostic/server/default-setup.js';

await launch();
Loading
Loading