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

test(next-drupal): reorganize Jest tests #675

Merged
merged 4 commits into from
Feb 21, 2024
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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{*.diff,*.patch}]
trim_trailing_whitespace = false
19 changes: 10 additions & 9 deletions examples/example-search-api/pages/advanced.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
DrupalNode,
getSearchIndexFromContext,
deserialize,
JsonApiSearchApiResponse,
DrupalSearchApiJsonApiResponse,
DrupalSearchApiFacet,
} from "next-drupal"
import { GetStaticPropsResult } from "next"
Expand Down Expand Up @@ -209,14 +209,15 @@ export default function AdvancedPage({
export async function getStaticProps(
context
): Promise<GetStaticPropsResult<AdvancedPageProps>> {
const results = await getSearchIndexFromContext<JsonApiSearchApiResponse>(
"property",
context,
{
deserialize: false,
params,
}
)
const results =
await getSearchIndexFromContext<DrupalSearchApiJsonApiResponse>(
"property",
context,
{
deserialize: false,
params,
}
)

return {
props: {
Expand Down
5 changes: 5 additions & 0 deletions modules/next/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ include:
# View these include files at https://git.drupalcode.org/project/gitlab_templates/
################
- project: $_GITLAB_TEMPLATES_REPO
# "ref" value can be:
# - Recommended (default) - `ref: $_GITLAB_TEMPLATES_REF` - The Drupal Association will update this value to the recommended tag for contrib.
# - Latest - `ref: main` - Get the latest additions and bug fixes as they are merged into the templates.
# - Minor or Major latests - `ref: 1.x-latest` or `ref: 1.0.x-latest` - Get the latest additions within a minor (mostly bugfixes) or major (bugs and new features).
# - Fixed tag - `ref: 1.0.1` - Set the value to a known tag. This will not get any updates.
ref: $_GITLAB_TEMPLATES_REF
file:
- '/includes/include.drupalci.main.yml'
Expand Down
56 changes: 28 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"format:check": "prettier --check .",
"phpcs": "./drupal/vendor/bin/phpcs -p -s --colors --standard=modules/next/phpcs.xml modules/next",
"test:next": "SIMPLETEST_DB=sqlite://localhost/:memory: ./drupal/vendor/bin/phpunit -c ./drupal/web/core modules/next",
"sync:modules": "./scripts/sync-repo.sh 1.0.x [email protected]:project/ \"modules/*\"",
"sync:modules": "./scripts/sync-repo.sh 2.x [email protected]:project/ \"modules/*\"",
"sync:examples": "./scripts/sync-repo.sh main [email protected]:chapter-three/next- \"examples/example-*\"",
"sync:starters": "./scripts/sync-repo.sh canary [email protected]:chapter-three/next-drupal- \"starters/*\"",
"sync:starters:release": "./scripts/sync-repo.sh canary,main [email protected]:chapter-three/next-drupal- \"starters/*\"",
Expand All @@ -32,46 +32,46 @@
"test:e2e:ci": "turbo run test:e2e:ci --parallel"
},
"resolutions": {
"@types/react": "17.0.68",
"@types/react-dom": "17.0.21"
"@types/react": "17.0.75",
"@types/react-dom": "17.0.25"
},
"devDependencies": {
"@actions/core": "^1.10.1",
"@babel/core": "^7.23.2",
"@babel/eslint-parser": "^7.22.15",
"@babel/plugin-transform-runtime": "^7.23.2",
"@babel/preset-env": "^7.23.2",
"@babel/preset-react": "^7.22.15",
"@commitlint/cli": "^18.6.0",
"@commitlint/config-conventional": "^17.8.0",
"@babel/core": "^7.23.9",
"@babel/eslint-parser": "^7.23.10",
"@babel/plugin-transform-runtime": "^7.23.9",
"@babel/preset-env": "^7.23.9",
"@babel/preset-react": "^7.23.3",
"@commitlint/cli": "^18.6.1",
"@commitlint/config-conventional": "^18.6.2",
"@next/eslint-plugin-next": "^12.3.4",
"@testing-library/cypress": "^8.0.7",
"@types/jest": "^27.5.2",
"@types/node": "^17.0.45",
"@types/react": "^17.0.68",
"@types/react-dom": "^17.0.21",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.19",
"@types/react": "^17.0.75",
"@types/react-dom": "^17.0.25",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^5.62.0",
"@typescript-eslint/parser": "^7.0.2",
"cypress": "^9.7.0",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-cypress": "^2.15.1",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-react": "^7.33.2",
"faker": "^6.6.6",
"husky": "^9.0.11",
"isomorphic-fetch": "^3.0.0",
"jest": "^27.5.1",
"lerna": "^8.0.1",
"lint-staged": "^15.0.2",
"prettier": "^3.0.3",
"jest": "^29.7.0",
"lerna": "^8.1.2",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"start-server-and-test": "^1.15.5",
"ts-jest": "^27.1.5",
"ts-node": "^10.9.1",
"tsup": "^7.3.0",
"turbo": "^1.10.15",
"typescript": "^5.2.2",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"tsup": "^8.0.2",
"turbo": "^1.12.4",
"typescript": "^5.3.3",
"uuid": "^9.0.1",
"yaml": "^2.3.3"
"yaml": "^2.3.4"
}
}
20 changes: 12 additions & 8 deletions packages/next-drupal-query/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest/presets/js-with-ts",
testEnvironment: "node",
setupFiles: ["dotenv/config"],
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
testMatch: ["**/tests/**/*.test.{ts,tsx}"],
globals: {
"ts-jest": {
isolatedModules: true,
},
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
isolatedModules: true,
},
],
},
testLocationInResults: true,
coverageProvider: "v8",
collectCoverage: true,
collectCoverageFrom: ["./src/**"],
coverageReporters: ["lcov", "text", "text-summary"],
coverageThreshold: {
global: {
statements: 85.11,
branches: 58,
statements: 84.22,
branches: 64.1,
functions: 70,
lines: 85.11,
lines: 84.22,
},
},
}
6 changes: 3 additions & 3 deletions packages/next-drupal-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export function withPagination(
}
}

if (typeof opts.page !== undefined && typeof opts.limit !== undefined) {
if (typeof opts.page !== "undefined" && typeof opts.limit !== "undefined") {
params.addPageLimit(opts.limit).addPageOffset(opts.page * opts.limit)
}

Expand Down Expand Up @@ -292,8 +292,8 @@ export function createQueries<Q extends Readonly<Queries<Q>>>(queries: Q) {
opts = massageRouteQuery(opts)

if (
typeof query["defaultOpts"] !== undefined &&
typeof opts !== undefined
typeof query["defaultOpts"] !== "undefined" &&
typeof opts !== "undefined"
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
opts = deepmerge(query["defaultOpts"], opts as any) as any
Expand Down
42 changes: 21 additions & 21 deletions packages/next-drupal-query/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`formatData it formats data 1`] = `
Object {
{
"name": "First Last",
}
`;

exports[`getData it accepts data options 1`] = `
Object {
{
"author": "shadcn",
"id": "ID",
"title": "Title of Article",
Expand All @@ -16,14 +16,14 @@ Object {
`;

exports[`getData it accepts placeholder options 1`] = `
Object {
{
"email": "[email protected]",
"name": "shadcn",
}
`;

exports[`getData it returns data for a query 1`] = `
Object {
{
"author": "shadcn",
"id": "id",
"title": "Title of Article",
Expand All @@ -32,52 +32,52 @@ Object {
`;

exports[`getData it returns data when both data and placeholder is defined 1`] = `
Object {
{
"email": "[email protected]",
}
`;

exports[`getData it returns formatted data if a formatter is defined 1`] = `
Object {
{
"email": "[email protected]",
"name": "First Last",
}
`;

exports[`getData it returns placeholder data if a query does not define data 1`] = `
Object {
{
"name": "shadcn",
}
`;

exports[`getParams it accepts params options 1`] = `
Object {
"fields": Object {
{
"fields": {
"resource": "foo,bar",
},
"filter": Object {},
"include": Array [],
"sort": Array [],
"filter": {},
"include": [],
"sort": [],
}
`;

exports[`getParams it allows params to be nested 1`] = `
Object {
"fields": Object {},
"filter": Object {},
{
"fields": {},
"filter": {},
"include": "field_image,uid",
"page": Object {
"page": {
"limit": 10,
},
"sort": Array [],
"sort": [],
}
`;

exports[`getParams it returns params for a query 1`] = `
Object {
"fields": Object {},
"filter": Object {},
{
"fields": {},
"filter": {},
"include": "field_image,uid",
"sort": Array [],
"sort": [],
}
`;
30 changes: 19 additions & 11 deletions packages/next-drupal/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
setupFiles: ["dotenv/config"],
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
testMatch: ["**/tests/**/*.test.{ts,tsx}"],
globals: {
"ts-jest": {
isolatedModules: true,
},
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
isolatedModules: true,
},
],
},
testLocationInResults: true,
coverageProvider: "v8",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The testLocationInResults flag shows the directory and filename of all the test.ts files which makes it way easier to organize the tests and quickly see where a failing test is.

collectCoverage: true,
collectCoverageFrom: ["./src/**"],
coveragePathIgnorePatterns: ["./src/get-*"],
coveragePathIgnorePatterns: [
"./src/deprecated/*",
"./src/deprecated.ts",
"./src/navigation.ts",
"./src/types/*",
],
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There's no reason to track code coverage of the deprecated API. And TypeScript removes import type lines from generated JS, so the JS code coverage tool thinks the type definition files are 0% used in tests.

coverageReporters: ["lcov", "text", "text-summary"],
coverageThreshold: {
global: {
// @TODO Make these thresholds strict once #608 is completed.
statements: 50, // 55.1,
branches: 80, // 84.16,
functions: 70, // 72.41,
lines: 50, // 55.1,
statements: 100,
branches: 100,
functions: 100,
lines: 100,
},
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yay! 100% code coverage.

},
}
2 changes: 1 addition & 1 deletion packages/next-drupal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"directory": "packages/next-drupal"
},
"devDependencies": {
"typescript": "^5.2.2"
"typescript": "^5.3.3"
},
"dependencies": {
"jsona": "^1.12.1",
Expand Down
Loading
Loading