Skip to content

Commit

Permalink
build, check and test working
Browse files Browse the repository at this point in the history
  • Loading branch information
ComLock committed Apr 12, 2024
1 parent b4dbf4f commit 7641da5
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 178 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[*.{json,yml}]
charset = utf-8
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ tasks.register('npmTest', NpmTask) {
environment = [
'FORCE_COLOR': 'true',
]
inputs.dir 'test'
inputs.dir 'src/jest'
outputs.dir 'coverage'
}

Expand Down
48 changes: 33 additions & 15 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import type { Config } from '@jest/types';
import {
AND_BELOW,
DIR_SRC,
DIR_SRC_ASSETS,
TEST_EXT
} from './tsup/constants';


const DIR_SRC_JEST = 'src/jest';
const DIR_SRC_JEST_CLIENT = `${DIR_SRC_JEST}/client`;
const DIR_SRC_JEST_SERVER = `${DIR_SRC_JEST}/server`;
const SOURCE_FILES = `*.{ts,tsx}`;
const TEST_EXT = `{spec,test}.{ts,tsx}`;
const TEST_FILES = `*.${TEST_EXT}`;
const TEST_MATCH_ASSETS = `<rootDir>/${DIR_SRC_ASSETS}/${AND_BELOW}/${TEST_FILES}`;


const commonConfig: Config.InitialProjectOptions = {
collectCoverageFrom: [
Expand All @@ -27,16 +27,27 @@ const clientSideConfig: Config.InitialProjectOptions = {
color: 'white',
name: 'CLIENT',
},

// A map from regular expressions to module names or to arrays of module
// names that allow to stub out resources, like images or styles with a
// single module.
// Use <rootDir> string token to refer to rootDir value if you want to use
// file paths.
// Additionally, you can substitute captured regex groups using numbered
// backreferences.
moduleNameMapper: {
'/assets/(.*)': `<rootDir>/${DIR_SRC}/assets/$1`,
},

testEnvironment: 'jsdom', // Run clientside tests with DOM globals such as document and window
testMatch: [
TEST_MATCH_ASSETS, // Every test file in the assets folder
`<rootDir>/test/client/${AND_BELOW}/${TEST_FILES}`
`<rootDir>/${DIR_SRC_JEST_CLIENT}/${AND_BELOW}/${TEST_FILES}`
],
transform: {
"^.+\\.(ts|js)x?$": [
'ts-jest',
{
tsconfig: 'test/client/tsconfig.json'
tsconfig: `${DIR_SRC_JEST_CLIENT}/tsconfig.json`
}
]
}
Expand Down Expand Up @@ -66,29 +77,36 @@ const serverSideConfig: Config.InitialProjectOptions = {
},
},

// A map from regular expressions to module names or to arrays of module
// names that allow to stub out resources, like images or styles with a
// single module.
// Use <rootDir> string token to refer to rootDir value if you want to use
// file paths.
// Additionally, you can substitute captured regex groups using numbered
// backreferences.
moduleNameMapper: {
'/controllers/(.*)': `<rootDir>/${DIR_SRC}/controllers/$1`,
'/lib/tutorial-jest/(.*)': `<rootDir>/${DIR_SRC}/lib/tutorial-jest/$1`,
},

// A list of paths to modules that run some code to configure or set up the
// testing environment. Each setupFile will be run once per test file. Since
// every test runs in its own environment, these scripts will be executed in
// the testing environment before executing setupFilesAfterEnv and before
// the test code itself.
setupFiles: [
'<rootDir>/test/server/setupFile.ts'
`<rootDir>/${DIR_SRC_JEST_SERVER}/setupFile.ts`
],

testEnvironment: 'node', // Run serverside tests without DOM globals such as document and window
testMatch: [
`<rootDir>/${DIR_SRC}/${AND_BELOW}/${TEST_FILES}`, // Every test file in src/main/resources
`<rootDir>/test/server/${AND_BELOW}/${TEST_FILES}`
],
testPathIgnorePatterns: [
"/node_modules/", // The default
`<rootDir>/${DIR_SRC_ASSETS}/`,
`<rootDir>/${DIR_SRC_JEST_SERVER}/${AND_BELOW}/${TEST_FILES}`
],
transform: {
"^.+\\.(ts|js)x?$": [
'ts-jest',
{
tsconfig: 'test/server/tsconfig.json'
tsconfig: `${DIR_SRC_JEST_SERVER}/tsconfig.json`
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion src/jest/client/HelloWorld.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@jest/globals';
import React from 'react';
import renderer from 'react-test-renderer';
import {HelloWorld} from './HelloWorld';
import {HelloWorld} from '/assets/js/HelloWorld';


describe('HelloWorld', () => {
Expand Down
41 changes: 34 additions & 7 deletions src/jest/client/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
{
"extends": "../../src/main/resources/assets/tsconfig.json",
"compilerOptions": {
"sourceMap": true, // Important to get correct line numbers when running coverage tests
"types": [
"jest", // This only works for test files in this folder
],
"extends": "../../main/resources/assets/tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
// A series of entries which re-map imports to lookup locations relative
// to the baseUrl if set, or to the tsconfig file itself otherwise.
"paths": {
"/assets/*": ["../../main/resources/assets/*"],
},
}
"sourceMap": true, // Important to get correct line numbers when running coverage tests

// By default all visible ”@types” packages are included in your
// compilation. Packages in node_modules/@types of any enclosing folder
// are considered visible. For example, that means packages within
// ./node_modules/@types/, ../node_modules/@types/,
// ../../node_modules/@types/, and so on.
// If types is specified, only packages listed will be included in the
// global scope.
// This feature differs from typeRoots in that it is about specifying
// only the exact types you want included, whereas typeRoots supports
// saying you want particular folders.
"types": [
"jest", // This only works for test files in this folder
],
},
"include": [
// "../../main/resources/assets/**/*.spec.ts",
// "../../main/resources/assets/**/*.spec.tsx",
// "../../main/resources/assets/**/*.test.ts",
// "../../main/resources/assets/**/*.test.tsx",
"./**/*.spec.ts",
"./**/*.spec.tsx",
"./**/*.test.ts",
"./**/*.test.tsx"
],
}
2 changes: 1 addition & 1 deletion src/jest/server/fibonacci.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
expect,
test as it
} from '@jest/globals';
import { fibonacci } from './fibonacci';
import { fibonacci } from '/lib/tutorial-jest/fibonacci';


describe('fibonacci', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/jest/server/getAppConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {App, Log} from '../../../../../test/server/global';
import type {App, Log} from './global';

import {
beforeAll,
Expand All @@ -25,7 +25,7 @@ describe('getAppConfig', () => {
globalThis.log.debug = () => {};
});
it('should return the application config', () => {
import('./getAppConfig').then(({getAppConfig}) => {
import('/lib/tutorial-jest/getAppConfig').then(({getAppConfig}) => {
expect(getAppConfig()).toEqual({
default: 'true',
key: 'value'
Expand Down
14 changes: 7 additions & 7 deletions src/jest/server/preview.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {ByteSource} from '@enonic-types/core';
import type {Log, Resolve} from '../../../../test/server/global';
import type {Log, Resolve} from './global';


import {
Expand All @@ -12,8 +12,8 @@ import {
libContent,
libPortal,
server,
} from '../../../../test/server/mockXP';
import '../../../../test/server/mockLibThymeleaf';
} from './mockXP';
import './mockLibThymeleaf';
import {readFileSync} from 'fs';
import {
join,
Expand Down Expand Up @@ -53,7 +53,7 @@ const personFolder = libContent.create({
});

const leaSeydouxJpg = libContent.createMedia({
data: readFileSync(join(__dirname, '../../../../test', 'Lea-Seydoux.jpg')) as unknown as ByteSource,
data: readFileSync(join(__dirname, '..', 'Lea-Seydoux.jpg')) as unknown as ByteSource,
name: 'Lea-Seydoux.jpg',
parentPath: personFolder._path,
mimeType: 'image/jpeg',
Expand All @@ -74,7 +74,7 @@ libContent.create({
});

const jeffreyWrightHpJpg = libContent.createMedia({
data: readFileSync(join(__dirname, '../../../../test', 'Jeffrey-Wright-hp.jpg')) as unknown as ByteSource,
data: readFileSync(join(__dirname, '..', 'Jeffrey-Wright-hp.jpg')) as unknown as ByteSource,
name: 'Jeffrey-Wright-hp.jpg',
parentPath: personFolder._path,
mimeType: 'image/jpeg',
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('preview', () => {
repositoryId: server.context.repository,
path: '/admin/site/preview/intro/draft/persons/lea-seydoux'
});
import('./preview').then(({get}) => {
import('/controllers/preview').then(({get}) => {
const response = get(libPortal.request);
expect(response).toEqual({
body: {
Expand All @@ -122,7 +122,7 @@ describe('preview', () => {
repositoryId: server.context.repository,
path: '/admin/site/preview/intro/draft/persons/jeffrey-wright'
});
import('./preview').then(({get}) => {
import('/controllers/preview').then(({get}) => {
const response = get(libPortal.request);
expect(response).toEqual({
body: {
Expand Down
2 changes: 1 addition & 1 deletion src/jest/server/sanitize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jest.mock('/lib/xp/common', () => ({

describe('sanitize', () => {
it('should return the first 10 numbers in the fibonacci sequence', () => {
import('./sanitize').then(({sanitize}) => {
import('/lib/tutorial-jest/sanitize').then(({sanitize}) => {
expect(sanitize("Piña CØLADÆ <script>alert('hi!');</script>"))
.toEqual('pina-coladae-script-alerthi-script');
});
Expand Down
2 changes: 1 addition & 1 deletion src/jest/server/setupFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {App, Log} from '../../test/server/global';
import type {App, Log} from './global';


// Avoid type errors
Expand Down
70 changes: 35 additions & 35 deletions src/jest/server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
{
"extends": "../../tsconfig.json",
"extends": "../../../tsconfig.json",

"compilerOptions": {
"sourceMap": true, // Important to get correct line numbers when running coverage tests
"types": [
"global", // Already added via typeRoots? nope, doesn't work that way
"jest", // This only works for test files in this folder
"node", // console
],
},

// Specifies an array of filenames or patterns to include in the program.
// These filenames are resolved relative to the directory containing the
// tsconfig.json file.
"include": [
"../../src/main/resources/**/*.spec.ts",
"../../src/main/resources/**/*.spec.tsx",
"../../src/main/resources/**/*.test.ts",
"../../src/main/resources/**/*.test.tsx",
"../../test/server/**/*.spec.ts",
"../../test/server/**/*.spec.tsx",
"../../test/server/**/*.test.ts",
"../../test/server/**/*.test.tsx"
"compilerOptions": {
"sourceMap": true, // Important to get correct line numbers when running coverage tests
"types": [
"global", // Already added via typeRoots? nope, doesn't work that way
"jest", // This only works for test files in this folder
"node", // console
],
},

// Specifies an array of filenames or patterns that should be skipped when
// resolving include.
// Important: exclude only changes which files are included as a result of
// the include setting. A file specified by exclude can still become part of
// your codebase due to an import statement in your code, a types inclusion,
// a /// <reference directive, or being specified in the files list.
// It is not a mechanism that prevents a file from being included in the
// codebase - it simply changes what the include setting finds.
"exclude": [
"../../src/main/resources/assets/**/*.*",
"../../src/main/resources/static/**/*.*",
],
}
// Specifies an array of filenames or patterns to include in the program.
// These filenames are resolved relative to the directory containing the
// tsconfig.json file.
"include": [
// "../../main/resources/**/*.spec.ts",
// "../../main/resources/**/*.spec.tsx",
// "../../main/resources/**/*.test.ts",
// "../../main/resources/**/*.test.tsx",
"./**/*.spec.ts",
"./**/*.spec.tsx",
"./**/*.test.ts",
"./**/*.test.tsx"
],

// Specifies an array of filenames or patterns that should be skipped when
// resolving include.
// Important: exclude only changes which files are included as a result of
// the include setting. A file specified by exclude can still become part of
// your codebase due to an import statement in your code, a types inclusion,
// a /// <reference directive, or being specified in the files list.
// It is not a mechanism that prevents a file from being included in the
// codebase - it simply changes what the include setting finds.
// "exclude": [
// "../../main/resources/assets/**/*.*",
// "../../main/resources/static/**/*.*",
// ],
}
46 changes: 26 additions & 20 deletions src/main/resources/assets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
{
"extends": "../../../../tsconfig.json",
"exclude": [
"../../../../node_modules/typescript/lib/lib.es5.d.ts",
],
"include": [
"./**/*.tsx",
"./**/*.ts"
],
"compilerOptions": {
"baseUrl": ".",
"jsx": "react",
"moduleResolution": "node",
"lib": [
"DOM",
"ES2020",
],
"paths": {},
"rootDir": ".",
"target": "ES2020" // Modern browsers
}
"extends": "../../../../tsconfig.json",
"exclude": [
"../../../../node_modules/typescript/lib/lib.es5.d.ts",
],
"include": [
"./**/*.tsx",
"./**/*.ts"
],
"compilerOptions": {
"baseUrl": ".",
"jsx": "react",
"moduleResolution": "node",
"lib": [
"DOM",
"ES2020",
],

// A series of entries which re-map imports to lookup locations relative
// to the baseUrl if set, or to the tsconfig file itself otherwise.
"paths": {
// Empty to avoid access to src/main/resources via /
},

"rootDir": ".",
"target": "ES2020" // Modern browsers
}
}
Loading

0 comments on commit 7641da5

Please sign in to comment.