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

Jest + RTL :: Testing with FHIRClient #166

Open
shaneajeffery opened this issue Mar 29, 2023 · 0 comments
Open

Jest + RTL :: Testing with FHIRClient #166

shaneajeffery opened this issue Mar 29, 2023 · 0 comments

Comments

@shaneajeffery
Copy link

Hello.

I am using the fhirClient package to pull R4 resources from our internal FHIR server. The initalization of the package is done in the main.tsx (top level file) and then the client that is generated is passed down in the app.tsx.

Given that the app.tsx has the client as a prop for the fhirClient that was generated, I need to be able to fake/mock the fhirClient and then pass it in.

Here is what I have thus far:

/* eslint-disable @typescript-eslint/no-var-requires */
import { uiProviderWrapper } from '@app/utils/jest-wrappers';
import { oauth2 as SMART } from 'fhirclient';

import { App } from '../app';

it('renders a treatment plan plus app container', () => {
  const client = SMART.client({
    serverUrl: '',
    tokenResponse: generateTokenResponse(),
  });

  console.log(client);

  const component = <App fhirClient={client} />;

  const screen = uiProviderWrapper(component);
  expect(screen.getByTestId('treatment-plan-container')).toBeInTheDocument();
});

const generateTokenResponse = (state = {}) => {
  const CLIENT_ID = 'my_web_app';
  const PATIENT_ID = 'b2536dd3-bccd-4d22-8355-ab20acdf240b';
  const ENCOUNTER_ID = 'e3ec2d15-4c27-4607-a45c-2f84962b0700';

  const resp = {
    access_token: 'omit'
    client_id: CLIENT_ID,
    code: 'omit',
    encounter: ENCOUNTER_ID,
    expires_in: 3600,
    id_token: 'omit'
    need_patient_banner: true,
    patient: PATIENT_ID,
    refresh_token: 'omit'
    scope: 'openid fhirUser offline_access user/*.* patient/*.* launch/encounter launch/patient profile',
    smart_style_url: 'https://launch.smarthealthit.org/smart-style.json',
    state,
    token_type: 'bearer' as 'bearer' | 'Bearer',
  };

  return resp;
};

When running this test, I get:

Details:

    /Users/shanejeffery/Desktop/mckesson/cds-txp/app/node_modules/jose/dist/browser/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { compactDecrypt } from './jwe/compact/decrypt.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      1 | /* eslint-disable @typescript-eslint/no-var-requires */
      2 | import { uiProviderWrapper } from '@app/utils/jest-wrappers';
    > 3 | import { oauth2 as SMART } from 'fhirclient';
        | ^
      4 |
      5 | import { App } from '../app';
      6 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1495:14)
      at Object.<anonymous> (node_modules/fhirclient/lib/security/server.js:8:16)
      at Object.<anonymous> (node_modules/fhirclient/lib/adapters/NodeAdapter.js:15:18)
      at Object.<anonymous> (node_modules/fhirclient/lib/entry/node.js:3:23)
      at Object.require (src/__tests__/app.test.tsx:3:1)

If I add that package (jose) to the transformIgnorePatterns in Jest, then it just opens up a bunch of other issues. It looks like Jest is trying to use the fhirclient package within a node environment instead of the jsdom that has been set in Jest.

Anyone been able to successfully get a test implemented with Jest with fhirclient?

Thanks!

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

No branches or pull requests

1 participant