-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure Jest to run existing unit tests (#9)
* refactor: Use explicit null check to get accurate return type * Install and configure jest to support existing unit tests * Document basic usage in README * v0.0.2-2 * Split e2e and jest tests into separate actions * Document basic development workflows * Add comment explaining use of double equals * Remove redundant ci:unit script Co-authored-by: Andrew Hyndman <ahyndman@dropbox.com>
- Loading branch information
Showing
8 changed files
with
1,739 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: Unit Tests | ||
on: | ||
push: | ||
branches: [ main, master ] | ||
pull_request: | ||
branches: [ main, master ] | ||
jobs: | ||
test: | ||
timeout-minutes: 60 | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v2 | ||
with: | ||
node-version: '16.x' | ||
- name: Install dependencies | ||
run: yarn | ||
- name: Run Jest tests | ||
run: yarn test:unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Overview | ||
|
||
> ⚠️ **This library is an experimental early-stage project. Use at your own risk.** | ||
`ttvc` provides an in-browser implementation of the VisuallyComplete metric suitable for field data collection (real-user monitoring). | ||
|
||
Visually Complete measures the moment in time when users perceive that all the visual elements of a page have completely loaded. Once the page has reached visually complete, nothing else should change in the viewport without the user’s input. | ||
|
||
# Get started | ||
|
||
This library is available from npm. Add it to your project using the `npm` or `yarn` package managers. | ||
|
||
``` | ||
$ npm install @dropbox-performance/ttvc | ||
``` | ||
|
||
``` | ||
$ yarn add @dropbox-performance/ttvc | ||
``` | ||
|
||
# Usage | ||
|
||
## Basic usage | ||
|
||
> ⚠️ **This API has not stabilized and is likely to change.** | ||
```js | ||
import {PageloadVisuallyCompleteCalculator} from '@dropbox-performance/ttvc'; | ||
|
||
// Initialize the calculator as early as possible in your page | ||
// load so that it can observe changes. | ||
const calculator = new PageLoadVisuallyCompleteCalculator(); | ||
calculator.start(); | ||
|
||
// Wait for the document to complete loading and compute the | ||
// timestamp of the last visible change. | ||
calculator.attemptMeasurement().then(timestamp => { | ||
console.log(timestamp); | ||
}); | ||
``` | ||
|
||
## Report metrics to a collection endpoint | ||
|
||
TBC | ||
|
||
## Record a PerformanceTimeline entry | ||
|
||
TBC | ||
|
||
# Browser Support | ||
|
||
TBC | ||
|
||
# Developing | ||
|
||
This package expects node version 16 or greater, and the `yarn` package manager. Once you have these prerequisites, install project dependencies with: | ||
|
||
``` | ||
yarn install | ||
``` | ||
|
||
## Building | ||
|
||
This project is developed with TypeScript. You can compile the TypeScript source files to JavaScript with: | ||
|
||
``` | ||
$ yarn build | ||
``` | ||
|
||
## Testing | ||
|
||
You can run all tests together with: | ||
|
||
``` | ||
$ yarn test | ||
``` | ||
|
||
### Testing with Jest | ||
|
||
To run only jest unit tests: | ||
|
||
``` | ||
$ yarn test:unit | ||
``` | ||
|
||
|
||
### Testing with Playwright | ||
|
||
Before running any playwright tests, you will need to install the default set of browsers: | ||
|
||
``` | ||
$ npx playwright install | ||
``` | ||
|
||
Run test suite: | ||
|
||
``` | ||
$ yarn test:e2e | ||
``` | ||
|
||
To manually test pages, start the test server. | ||
|
||
``` | ||
$ yarn test:server | ||
``` | ||
|
||
Then navigate to a test case in your favorite browser. e.g. http://localhost:3000/test/ajax-mutation/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'jsdom', | ||
testMatch: ['**/*.jest.ts'], | ||
setupFilesAfterEnv: ['./test/jest.setup.js'], | ||
// ignore .js extensions in import statements | ||
moduleNameMapper: {'(.+)\\.js': '$1'}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// TODO: Consider rewriting unit tests so that they do not require these global stubs | ||
|
||
// stub the MutationObserver and IntersectionObserver interfaces | ||
global.MutationObserver = class { | ||
constructor(callback) {} | ||
disconnect() {} | ||
observe(element, initObject) {} | ||
}; | ||
|
||
global.IntersectionObserver = class { | ||
constructor(callback) {} | ||
disconnect() {} | ||
observe() {} | ||
takeRecords() {} | ||
unobserve() {} | ||
}; | ||
|
||
// jsdom has no performance.navigation. This is an attempt to | ||
// give it something to use instead | ||
window.performance.navigation = { | ||
TYPE_NAVIGATE: 0, | ||
TYPE_RELOAD: 1, | ||
TYPE_BACK_FORWARD: 2, | ||
TYPE_RESERVED: 255, | ||
redirectCount: 0, | ||
type: 0, // TYPE_NAVIGATE | ||
}; | ||
|
||
// some tests need this | ||
window.performance.timing = { | ||
// this is an arbitrary timestamp sometime in Feb 2018. We expect tests | ||
// shouldn't care what year / day it is, but we used a fixed one for | ||
// repeatability. | ||
navigationStart: 1518224417028, | ||
}; |
Oops, something went wrong.