Skip to content

Commit

Permalink
@W-17592100 chore: run unit tests in production mode (#5270)
Browse files Browse the repository at this point in the history
* ci: run unit tests in prod mode

* test(engine-dom): skip testing dev mode feature in prod mode

* test(ssr-compiler): avoid checking dev mode error text in prod mode

* test(engine-server): skip fixture tests in prod mode

there are a lot of failures that we'll get to later

* chore(tests): support NODE_ENV=production

* Apply suggestions from code review

* chore(coverage): bump minimum function threshold for SSR packages

* chore: remove unused file

* test(coverage): ignore ssr-client-utils and bump thresholds for SSR

* ci: don't generate coverage for prod mode

Coverage will always be lower because we have a ton of
`NODE_ENV !== production checks`, but no `===` checks.

---------

Co-authored-by: Matheus Cardoso <[email protected]>
  • Loading branch information
wjhsf and cardoso authored Mar 7, 2025
1 parent 51b9797 commit 0768b67
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ jobs:
run: yarn test:types
- name: Run unit tests
run: yarn test:ci
- name: Run unit tests in production mode
run: yarn test:production --no-watch
- name: Upload unit test coverage report
uses: actions/upload-artifact@v4
with:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"copy-fork": "./scripts/tasks/unsafe-external-contributor-ci-workaround.sh",
"dev": "nx run-many --target=dev --all --parallel=999 --exclude=@lwc/perf-benchmarks,@lwc/perf-benchmarks-components,@lwc/integration-tests",
"test": "vitest --workspace vitest.workspace.mjs",
"test:production": "VITE_NODE_ENV=production vitest --workspace vitest.workspace.mjs",
"test:bespoke": "nx run-many --target=test",
"test:debug": "vitest --workspace vitest.workspace.mjs --inspect-brk --no-file-parallelism",
"test:ci": "vitest run --workspace vitest.workspace.mjs --coverage",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { LOWEST_API_VERSION } from '@lwc/shared';

// it needs to be imported from the window, otherwise the checks for associated vms is done against "@lwc/engine-core"
const LightningElementFormatter = (globalThis as any)['devtoolsFormatters'].find((f: any) => {
const LightningElementFormatter = (globalThis as any)['devtoolsFormatters']?.find((f: any) => {
return f.name === 'LightningElementFormatter';
});

Expand All @@ -33,8 +33,9 @@ class WireAdapter {
disconnect() {}
}

describe('Lightning Element formatter', () => {
const { header } = LightningElementFormatter;
// LightningElementFormatter is not exposed in prod mode
describe.skipIf(process.env.NODE_ENV === 'production')('Lightning Element formatter', () => {
const header = LightningElementFormatter?.header;

it('should not contain body', () => {
expect(LightningElementFormatter.hasBody()).toBe(false);
Expand Down
7 changes: 6 additions & 1 deletion packages/@lwc/engine-server/src/__tests__/fixtures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { testFixtureDir, formatHTML, pluginVirtual } from '@lwc/test-utils-lwc-i
import { setFeatureFlagForTest } from '../index';
import type { LightningElementConstructor } from '@lwc/engine-core/dist/framework/base-lightning-element';
import type { RollupLwcOptions } from '@lwc/rollup-plugin';
import type { FeatureFlagName } from '@lwc/features/dist/types';

vi.mock('lwc', async () => {
const lwcEngineServer = await import('../index');
Expand All @@ -33,6 +34,9 @@ interface FixtureConfig {

/** Props to provide to the root component. */
props?: Record<string, string>;

/** Feature flags to enable for the test. */
features: FeatureFlagName[];
}

async function compileFixture({
Expand Down Expand Up @@ -162,7 +166,8 @@ function testFixtures(options?: RollupLwcOptions) {
);
}

describe.concurrent('fixtures', () => {
// TODO [#5134]: Enable these tests in production mode
describe.skipIf(process.env.NODE_ENV === 'production').concurrent('fixtures', () => {
beforeAll(() => {
// ENABLE_WIRE_SYNC_EMIT is used because this mimics the behavior for LWR in SSR mode. It's also more reasonable
// for how both `engine-server` and `ssr-runtime` behave, which is to use sync rendering.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe.each(
`;
const doReplacement = () => tmpl(b.literal('I am not an identifier') as any);
expect(doReplacement).toThrow(
'Validation failed for templated node. Expected type identifier, but received Literal.'
/^Validation failed for templated node\. Expected type .+?, but received Literal\.$/
);
});
});
Expand Down
12 changes: 0 additions & 12 deletions packages/@lwc/ssr-compiler/src/optimizer.ts

This file was deleted.

4 changes: 4 additions & 0 deletions packages/@lwc/ssr-runtime/src/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

/* v8 ignore start */

// Stubs for all the un-implemented exports from @lwc/engine-server

export function api(..._: unknown[]): never {
Expand Down Expand Up @@ -216,3 +218,5 @@ export const renderer = {
*/
// A real stub, not a "not implemented" one! 😯
export const hot = undefined;

/* v8 ignore stop */
8 changes: 5 additions & 3 deletions vitest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default defineConfig({
'**/packages/@lwc/engine-dom/**',
'**/packages/@lwc/engine-core/**',
'**/packages/@lwc/synthetic-shadow/**',
// TODO [#5272]: add tests
'**/packages/@lwc/ssr-client-utils/**',
// Ignore test packages
'**/packages/@lwc/integration-karma/**',
'**/packages/@lwc/integration-tests/**',
Expand All @@ -36,9 +38,9 @@ export default defineConfig({
// SSR compiler/runtime is relatively newer, so has lower thresholds for now
'**/packages/@lwc/ssr-*/**': {
branches: 90,
functions: 60,
lines: 85,
statements: 85,
functions: 90,
lines: 90,
statements: 90,
},

'!**/packages/@lwc/ssr-*/**': {
Expand Down
8 changes: 7 additions & 1 deletion vitest.shared.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import inspector from 'node:inspector';
import process from 'node:process';
import { defineConfig } from 'vitest/config';
import pkg from './package.json';

export default defineConfig({
test: {
// We can't set `NODE_ENV` directly (i.e. `NODE_ENV=production yarn test`), because some packages
// set their env to jsdom, and NODE_ENV=production + jsdom causes `node:` imports to break
env: {
NODE_ENV: process.env.VITE_NODE_ENV,
},
// Don't time out if we detect a debugger attached
testTimeout: inspector.url()
? // Largest allowed delay, see https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#maximum_delay_value
Expand All @@ -28,6 +33,7 @@ export default defineConfig({
'rollup-plugin',
'shared',
'signals',
'ssr-client-utils',
'ssr-compiler',
'ssr-runtime',
'style-compiler',
Expand Down

0 comments on commit 0768b67

Please sign in to comment.