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

feat(*): typescript prettier vitest #57

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

code-forger
Copy link
Member

Convert this codebase to typescript (from js), prettier (from nothing) and vitest (from jest)

@@ -43,7 +43,7 @@ reducer.buildInitialState = buildInitialState;
```
Copy link
Member Author

Choose a reason for hiding this comment

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

I will add the new documentation for how to use this library with typescript before this PR can be merged

});
}

export function runVitruviusImmutableTests(
Copy link
Member Author

Choose a reason for hiding this comment

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

Its a shame that this function is bifurcated for the sake of the two different type of Vitruvius. However the gymnastics that would be required to do this in a single function like it was before are not worth it for a test file.

Copy link
Member

Choose a reason for hiding this comment

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

aww, by TS overrides are sooooo fun 🤡

other: otherReducer,
static: staticReducer,
});
const store = configureStore({ reducer }, reducer.buildInitialState(locals));
Copy link
Member Author

Choose a reason for hiding this comment

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

Note, this test did not make it into the other file because TS does not allow it. This is because configureStore expects 1 parameter and we are passing 2. I can not find any reference online to this function ever taking 2 parameters.

import collectBuiltState, { VitruviusImmutableReducers } from './collectBuiltState';

// internal type for use before assigning buildInitial state
type VitruviusImmutableOptionalCombinedReducer<Locals> = ReturnType<typeof combineReducers> & {
Copy link
Member Author

Choose a reason for hiding this comment

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

Ironically, the use of ReturnType<typeof combineReducers> actually weakens the types of vitruvius. This is because redux-immutable does not preserve the types of the combined reducers. Therefore when using the immutable variation of vitruvius, callers will not be able to rely on typescript when accessing the resultant store.

I looked briefly into preserving the types of the reducers within vitruvius itself, but it is extremely complex. I recommend we proceed with the above types, and re-visit this hole in the type chain later.

guym4c
guym4c previously approved these changes Nov 13, 2024
Copy link
Member

@smackfu smackfu left a comment

Choose a reason for hiding this comment

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

Need to circle back to review the code more.

vitest.config.js Outdated
@@ -0,0 +1,14 @@
import { defineConfig } from 'vitest/config';
Copy link
Member

@smackfu smackfu Nov 13, 2024

Choose a reason for hiding this comment

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

Maybe make the vitest.config.js a typescript file?

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67

@@ -5,27 +5,18 @@
"main": "lib/index.js",
Copy link
Member

Choose a reason for hiding this comment

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

Think we need to add a types key.

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67

package.json Outdated
"test:lockfile": "lockfile-lint -p package-lock.json -t npm -a npm -o https: -c -i",
"test:unit": "jest",
"test:unit": "npm run build --ignore-scripts && vitest --run",
Copy link
Member

Choose a reason for hiding this comment

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

Can you make the build a "pretest:unit" script?

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67

package.json Outdated
"redux": "^5.0.1",
"redux-immutable": "^4.0.0",
"rimraf": "^5.0.5"
"rimraf": "^5.0.5",
"typescript": "^5.4.4",
Copy link
Member

Choose a reason for hiding this comment

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

Typescript doesn't follow semver because every change is breaking.

Suggested change
"typescript": "^5.4.4",
"typescript": ">=5.4.4",

Copy link
Member

Choose a reason for hiding this comment

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

wouldn't it be better to pin it then?

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps ~? Or are even patch versions breaking?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah since it's a dev dep it honestly doesn't really matter.

Copy link
Member

Choose a reason for hiding this comment

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

I just like pointing it out.

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67

vitest.config.js Outdated
Copy link
Member

Choose a reason for hiding this comment

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

I wonder why this file didn't get flagged by the issue this PR fixes:
americanexpress/eslint-config-amex#130

Copy link
Member

Choose a reason for hiding this comment

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

This file is being ignored currently

#57 (comment)

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67

tsconfig.json Outdated
"webworker.importscripts",
"scripthost"
],
"jsx": "react",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"jsx": "react",
"jsx": "react",

Is this needed? this package doesn't contain any react code

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67

Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this include a build step now?

Copy link
Member

Choose a reason for hiding this comment

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

We already had an existing build step, its covered by:

"prepublishOnly": "npm run build",

Though, personally, I prefer prepack over prepublishOnly

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I would suggest prepack. Otherwise when you run npm pack it doesn't rebuild.

@@ -1,3 +1,4 @@
{
"presets": ["amex"]
"presets": ["amex"],
"plugins": ["@babel/plugin-transform-typescript"]
Copy link
Member

Choose a reason for hiding this comment

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

Does babel-preset-amex need TS support?

e.g.

{
  "presets": ["amex/ts"]
}

Copy link
Member

Choose a reason for hiding this comment

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

honestly, we maybe don't even need a separate file. could just use overrides for .ts and .tsx

.eslintignore Outdated
@@ -1,3 +1,5 @@
lib
node_modules
test-results
vitest.config.js
Copy link
Member

Choose a reason for hiding this comment

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

Why ignore this?

Copy link
Member

Choose a reason for hiding this comment

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

Aha! That's why it didn't run into this issue:
americanexpress/eslint-config-amex#130

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67

package.json Outdated
"amex-jest-preset": "^7.0.0",
"@types/redux-immutable": "^4.0.6",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@vitest/coverage-v8": "^2.1.4",
"babel-preset-amex": "^4.0.3",
"coveralls": "^3.1.1",
"eslint": "^8.56.0",
"eslint-config-amex": "^16.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"eslint-config-amex": "^16.0.0",
"eslint-config-amex": "^16.2.0",

May as well 🤷🏼‍♀️

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67

package.json Outdated
@@ -5,27 +5,18 @@
"main": "lib/index.js",
"scripts": {
"clean": "rimraf lib",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"clean": "rimraf lib",
"rimraf": "node -e \"process.argv.slice(1).forEach(path => require('node:fs').rmSync(path, { recursive: true, force: true }));\" --",
"clean": "npm run rimraf lib",

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67

package.json Outdated
"redux": "^5.0.1",
"redux-immutable": "^4.0.0",
"rimraf": "^5.0.5"
"rimraf": "^5.0.5",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"rimraf": "^5.0.5",

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67

package.json Outdated
"babel-preset-amex": "^4.0.3",
"coveralls": "^3.1.1",
"eslint": "^8.56.0",
"eslint-config-amex": "^16.0.0",
"eslint-import-resolver-typescript": "^3.6.3",
"eslint-plugin-jest": "^27.6.3",
Copy link
Member

Choose a reason for hiding this comment

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

Is this still needed?

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67


exports[`vitruvius should return an initialState that is acceptable to redux toolkit's configureStore 1`] = `
Copy link
Member

Choose a reason for hiding this comment

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

Intentionally deleted snapshot?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in
5990a67

__tests__/collectBuiltState.spec.ts Show resolved Hide resolved
other: otherReducer,
static: staticReducer,
});
const store = configureStore({ reducer }, reducer.buildInitialState(locals));
Copy link
Member

Choose a reason for hiding this comment

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

I think it should be:

const store = configureStore({ 
  reducer,
  preloadedState: reducer.buildInitialState(locals)
});

Copy link
Member Author

Choose a reason for hiding this comment

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

you are right

Copy link
Member Author

@code-forger code-forger Dec 10, 2024

Choose a reason for hiding this comment

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

addressed in 5990a67

});
}

export function runVitruviusImmutableTests(
Copy link
Member

Choose a reason for hiding this comment

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

aww, by TS overrides are sooooo fun 🤡

"prebuild": "npm run test:lint && npm run clean",
"build": "babel src -d lib --copy-files",
"prebuild": "npm run test:lint",
"build": "babel src -d lib --extensions '.ts' --copy-files & tsc",
"test": "npm run test:lint && npm run test:unit",
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

The idea is that the types are implicitly tested by the incredibly strict configuration we have for tsc and eslint.

I would be happy to investigate tsd as a follow up piece of work

@10xLaCroixDrinker
Copy link
Member

A little feedback on the commit message. It is debatable whether this is a feature, but if it is, only the addition of types is a feature and that's all that should be in the head of the message. Details about refactors and tests should be in the body. This will make for a better changelog upon release

@code-forger
Copy link
Member Author

@10xLaCroixDrinker

A little feedback on the commit message....

did an amend to fix, look good now?

@smackfu
Copy link
Member

smackfu commented Dec 11, 2024

@code-forger this isn't working for me. I'm not getting the types in the package:

Screenshot 2024-12-11 at 12 04 09 PM

As a result getting this error:

src/ducks.ts:1:29 - error TS7016: Could not find a declaration file for module '@americanexpress/vitruvius/immutable'.

@code-forger
Copy link
Member Author

@smackfu an npm run build produces this output for me
image

@code-forger
Copy link
Member Author

@smackfu i tested with pack, this was an npm ignore issue, fixed in 7e83db3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants