Skip to content

DF-1509: Convert the server, page controller and component tests to JavaScript with JSDoc - #506

Open
alexluckett wants to merge 7 commits into
chore/df-1509-lint-configfrom
chore/df-1509-tests
Open

alexluckett wants to merge 7 commits into
chore/df-1509-lint-configfrom
chore/df-1509-tests

Conversation

@alexluckett

@alexluckett alexluckett commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Proposed change

Part of the TypeScript to JavaScript migration (v6), this PR is stacked on the lint settings PR and converts 60 test files, together with the test/helpers/validation-helpers helper, to JavaScript with JSDoc types. It covers the server, route, repeater and service tests, the page controller tests, and the component and component helper tests; no source files change. Converting the tests before the code they test means that each later source PR is checked by tests that have not changed. Each area is a separate commit, for reviewers who would rather read it in steps.

  • Type imports have moved to a single @import block at the end of each file, annotations are now @type, @param and @returns, as casts are JSDoc casts, and satisfies is @satisfies.
  • Where a line started with a cast on a Jest mock, it now uses jest.mocked(x), so ;(getPage as jest.Mock).mockReturnValue(page) becomes jest.mocked(getPage).mockReturnValue(page). Prettier, configured without semicolons, cannot keep a JSDoc cast at the start of a line. When a typed mock would reject a partial value, the cast goes inside the call instead: jest.mocked(/** @type {jest.Mock} */ (getCacheService)). Because jest.mocked simply returns its argument, this is the only change at runtime (in routes/index.test and postcode-lookup.test).
  • toEqual<FormSubmissionError[]>(…) is now toEqual(/** @satisfies {FormSubmissionError[]} */ (…)), which keeps the same check, since JavaScript has no way to pass a type argument.
  • A few types can be read by tsc but not by the JSDoc lint parser: the method signatures in TestableFileUploadPageController and in two casts in DatePartsField.test, and the mapped type in validation-helpers. These sit inside a jsdoc/valid-types disable block.
  • LocationFieldHelpers.test.js has one new lint disable. @typescript-eslint/no-base-to-string checks the type of label before the JSDoc cast is applied, so it reports label.toString() even though the cast gives label a toString method; the disable comment explains this.
  • TelephoneNumberField.test had an @import comment part-way through the file. TypeScript ignored it but JavaScript does not, so it has been merged into the block at the end of the file.
  • In helpers/state.test, const params = {} now has /** @type {FormQuery} */. In JavaScript an empty object literal can grow, so without the annotation tsc will not accept it as the FormQuery that stripParam expects.
  • Three eslint-disable comments for rules that only apply to TypeScript have been removed, as lint reports them as unused in JavaScript.
  • The @ts-expect-error comments on partial mocks are unchanged, because tsc still reports an error under each of them.
  • PaymentField.test.js includes the getReference mocks added on main with the payment reference fix.
  • jest.mock('…/x.ts') paths that point at source files still written in TypeScript are left as they are; the source PRs will update them.

Apart from the jest.mocked calls, each converted test compiles to the same code as before. The full test suite gives the same results as on main (2,583 passed, 13 skipped).

Jira ticket: DF-1509

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Misc. (documentation, build updates, etc)

Checklist

  • You have executed this code locally and it performs as expected.
  • You have added tests to verify your code works.
  • You have added code comments and JSDoc, where appropriate.
  • There is no commented-out code.
  • You have added developer docs in README.md and docs/* (where appropriate, e.g. new features).
  • The tests are passing (npm run test).
  • The linting checks are passing (npm run lint).
  • The code has been formatted (npm run format).

The AutocompleteField, CheckboxesField, DatePartsField, DeclarationField,
Details, EastingNorthingField, EmailAddressField, custom validation message
and default translator tests are now .js files typed with JSDoc and checked
by tsc.
The FileUploadField, GeospatialField, HiddenField, Html, InsetText,
LatLongField, List, LocationFieldBase, LocationFieldHelpers and Markdown
tests are now .js files typed with JSDoc and checked by tsc.
@alexluckett
alexluckett added this pull request to stack #513 November 24, 2026 21:27
@sonarqubecloud

Copy link
Copy Markdown

expect(errors.advancedSettingsErrors).not.toBeEmpty()
})

/* eslint-disable jsdoc/valid-types -- the JSDoc type parser rejects a method signature in a type literal; tsc reads it */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's annoying that you have to do this

function buildRequest(path: string, query = {}): FormContextRequest {
/**
* @param {string} path
* @returns {FormContextRequest}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing JSdoc for query param


jest.mock('~/src/server/plugins/engine/helpers')

function buildMockModel(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Params JSdoc missing?


jest.mock('~/src/server/plugins/engine/services/formsService.js')
jest.mock('~/src/server/plugins/engine/services/uploadService.js')
jest.mock('~/src/server/plugins/engine/helpers.ts', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be a .js file, or is that in a later PR?

@jbarnsley10 jbarnsley10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM - took a loooong time to go through.
A few tiny comments but those issues are clearly not causing a lint error.
One suggestion is to alphabetic order the footer imports on each line (not necessarily the line order, just the order of types listed per line being imported). Apparently there are 6 files where the types are out of order

map/routes/index.js:277
postcode-lookup/routes/index.js:336
formsService.js:48
geospatial-map.js:1137
geospatial-map.js:1141
file-form-service.js:178

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants