amplify-backend icon indicating copy to clipboard operation
amplify-backend copied to clipboard

[GEN2 backend]Jest testing issue: No context value present for amplify-backend-namespace key

Open MyNameIsTakenOMG opened this issue 1 year ago • 4 comments

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

REST API

Amplify Version

v6

Amplify Categories

No response

Backend

CDK

Environment information

# Put output below this line
System:
    OS: macOS 14.5
    CPU: (8) arm64 Apple M1 Pro
    Memory: 299.17 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.15.0 - /opt/homebrew/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 10.7.0 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 126.1.67.115
    Chrome: 126.0.6478.127
    Safari: 17.5
  npmPackages:
    @ampproject/toolbox-optimizer:  undefined ()
    @aws-amplify/backend: ^1.0.0 => 1.0.0 
    @aws-amplify/backend-cli: ^1.0.1 => 1.0.1 
    @aws-amplify/ui-react: ^6.1.9 => 6.1.9 
    @aws-amplify/ui-react-internal:  undefined ()
    @babel/core:  undefined ()
    @babel/runtime:  7.15.4 
    @edge-runtime/cookies:  3.4.1 
    @edge-runtime/ponyfill:  2.4.0 
    @edge-runtime/primitives:  3.1.1 
    @hapi/accept:  undefined ()
    @mswjs/interceptors:  undefined ()
    @napi-rs/triples:  undefined ()
    @next/font:  undefined ()
    @next/react-dev-overlay:  undefined ()
    @opentelemetry/api:  undefined ()
    @segment/ajv-human-errors:  undefined ()
    @testing-library/jest-dom: ^6.4.6 => 6.4.6 
    @testing-library/react: ^16.0.0 => 16.0.0 
    @types/jest: ^29.5.12 => 29.5.12 
    @types/node: ^20 => 20.12.8 
    @types/react: ^18 => 18.3.1 
    @types/react-dom: ^18 => 18.3.0 
    @vercel/nft:  undefined ()
    @vercel/og:  undefined ()
    acorn:  undefined ()
    amphtml-validator:  undefined ()
    anser:  undefined ()
    arg:  undefined ()
    assert:  undefined ()
    async-retry:  undefined ()
    async-sema:  undefined ()
    aws-amplify: ^6.3.8 => 6.3.8 
    aws-amplify/adapter-core:  undefined ()
    aws-amplify/analytics:  undefined ()
    aws-amplify/analytics/kinesis:  undefined ()
    aws-amplify/analytics/kinesis-firehose:  undefined ()
    aws-amplify/analytics/personalize:  undefined ()
    aws-amplify/analytics/pinpoint:  undefined ()
    aws-amplify/api:  undefined ()
    aws-amplify/api/server:  undefined ()
    aws-amplify/auth:  undefined ()
    aws-amplify/auth/cognito:  undefined ()
    aws-amplify/auth/cognito/server:  undefined ()
    aws-amplify/auth/enable-oauth-listener:  undefined ()
    aws-amplify/auth/server:  undefined ()
    aws-amplify/data:  undefined ()
    aws-amplify/data/server:  undefined ()
    aws-amplify/datastore:  undefined ()
    aws-amplify/in-app-messaging:  undefined ()
    aws-amplify/in-app-messaging/pinpoint:  undefined ()
    aws-amplify/push-notifications:  undefined ()
    aws-amplify/push-notifications/pinpoint:  undefined ()
    aws-amplify/storage:  undefined ()
    aws-amplify/storage/s3:  undefined ()
    aws-amplify/storage/s3/server:  undefined ()
    aws-amplify/storage/server:  undefined ()
    aws-amplify/utils:  undefined ()
    babel-packages:  undefined ()
    browserify-zlib:  undefined ()
    browserslist:  undefined ()
    buffer:  undefined ()
    bytes:  undefined ()
    ci-info:  undefined ()
    cli-select:  undefined ()
    client-only:  0.0.1 
    comment-json:  undefined ()
    compression:  undefined ()
    conf:  undefined ()
    constants-browserify:  undefined ()
    content-disposition:  undefined ()
    content-type:  undefined ()
    cookie:  undefined ()
    cross-spawn:  undefined ()
    crypto-browserify:  undefined ()
    css.escape:  undefined ()
    data-uri-to-buffer:  undefined ()
    debug:  undefined ()
    devalue:  undefined ()
    domain-browser:  undefined ()
    edge-runtime:  undefined ()
    events:  undefined ()
    find-cache-dir:  undefined ()
    find-up:  undefined ()
    fresh:  undefined ()
    get-orientation:  undefined ()
    glob:  undefined ()
    gzip-size:  undefined ()
    http-proxy:  undefined ()
    http-proxy-agent:  undefined ()
    https-browserify:  undefined ()
    https-proxy-agent:  undefined ()
    icss-utils:  undefined ()
    ignore-loader:  undefined ()
    image-size:  undefined ()
    is-animated:  undefined ()
    is-docker:  undefined ()
    is-wsl:  undefined ()
    jest: ^29.7.0 => 29.7.0 
    jest-environment-jsdom: ^29.7.0 => 29.7.0 
    jest-worker:  undefined ()
    json5:  undefined ()
    jsonwebtoken:  undefined ()
    loader-runner:  undefined ()
    loader-utils:  undefined ()
    lodash.curry:  undefined ()
    lru-cache:  undefined ()
    micromatch:  undefined ()
    mini-css-extract-plugin:  undefined ()
    nanoid:  undefined ()
    native-url:  undefined ()
    neo-async:  undefined ()
    next: 13.5.4 => 13.5.4 
    node-fetch:  undefined ()
    node-html-parser:  undefined ()
    ora:  undefined ()
    os-browserify:  undefined ()
    p-limit:  undefined ()
    path-browserify:  undefined ()
    platform:  undefined ()
    postcss-flexbugs-fixes:  undefined ()
    postcss-modules-extract-imports:  undefined ()
    postcss-modules-local-by-default:  undefined ()
    postcss-modules-scope:  undefined ()
    postcss-modules-values:  undefined ()
    postcss-preset-env:  undefined ()
    postcss-safe-parser:  undefined ()
    postcss-scss:  undefined ()
    postcss-value-parser:  undefined ()
    process:  undefined ()
    punycode:  undefined ()
    querystring-es3:  undefined ()
    raw-body:  undefined ()
    react: ^18 => 18.3.1 
    react-builtin:  undefined ()
    react-dom: ^18 => 18.3.1 
    react-dom-builtin:  undefined ()
    react-dom-experimental-builtin:  undefined ()
    react-experimental-builtin:  undefined ()
    react-is:  18.2.0 
    react-refresh:  0.12.0 
    react-server-dom-webpack-builtin:  undefined ()
    react-server-dom-webpack-experimental-builtin:  undefined ()
    regenerator-runtime:  0.13.4 
    sass-loader:  undefined ()
    scheduler-builtin:  undefined ()
    scheduler-experimental-builtin:  undefined ()
    schema-utils:  undefined ()
    semver:  undefined ()
    send:  undefined ()
    server-only:  0.0.1 
    setimmediate:  undefined ()
    shell-quote:  undefined ()
    source-map:  undefined ()
    stacktrace-parser:  undefined ()
    stream-browserify:  undefined ()
    stream-http:  undefined ()
    string-hash:  undefined ()
    string_decoder:  undefined ()
    strip-ansi:  undefined ()
    superstruct:  undefined ()
    tar:  undefined ()
    terser:  undefined ()
    text-table:  undefined ()
    timers-browserify:  undefined ()
    ts-jest: ^29.1.5 => 29.1.5 
    tty-browserify:  undefined ()
    typescript: ^5.3.2 => 5.4.5 (4.4.4, 4.9.5)
    ua-parser-js:  undefined ()
    undici:  undefined ()
    unistore:  undefined ()
    util:  undefined ()
    vm-browserify:  undefined ()
    watchpack:  undefined ()
    web-vitals:  undefined ()
    webpack:  undefined ()
    webpack-sources:  undefined ()
    ws:  undefined ()
  npmGlobalPackages:
    aws-cdk: 2.147.0
    corepack: 0.28.1
    npm: 10.7.0
    ts-node: 10.9.1
    typescript: 5.1.3
    yarn: 1.22.19

Describe the bug

I followed the amplify gen2 doc to add REST api to the backend, then I wrote a simple test suite to test if resources were provisioned correctly. This is the test suite: Screenshot 2024-07-03 at 10 15 15 AM

But when I first ran the test, I got an issue SyntaxError: Unexpected token 'export', and screenshot is below: Screenshot 2024-07-02 at 6 36 39 PM

So, I went to jest.config.ts and re-wrote the next/jest config like this:

module.exports = async () => ({
  ...(await createJestConfig(customConfig)()),
  transformIgnorePatterns: ['node_modules/?!(@aws-amplify/backend)'],
});

. Then I ran the test again, this time it gave me a different issue: Screenshot 2024-07-02 at 6 43 26 PM I am not sure what does this No context value present for amplify-backend-namespace key really mean, but my goal was to run some quick tests to make sure the underlying resources are provisioned correctly so that I don't have to do it by writing some scripts using aws cli or aws sdk after the whole backend environment is setup and running, instead I could focus on e2e tests or something else.

Expected behavior

The tests should be running successfully.

Reproduction steps

  1. clone the example repo:
git clone https://github.com/MyNameIsTakenOMG/amplify-next-backend-testing.git
  1. install the dependencies:
npm i
  1. run the test:
npm run test
  1. Please note: I did not configure the nextjs frontend to connect with the amplify backend, instead I simply just took advantage of nextjs auto-configuration of jest so that I could run some tests for the backend resources.

Code Snippet

// Put your code below this line.

Log output

// Put your logs below this line


aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

MyNameIsTakenOMG avatar Jul 02 '24 23:07 MyNameIsTakenOMG

Hello, @MyNameIsTakenOMG 👋. This looks to be specific to Jest rather than a bug with the Amplify library, but want to see if we can get you unblocked! There's been similar issues that reported getting this error with some potential workarounds in issue aws-amplify/amplify-js#11435 (here and here) where there were updates made to the transformIgnorePatters to add modules to resolve. Can you let me know if anything along those lines helps resolve the issue for you?

cwomack avatar Jul 04 '24 01:07 cwomack

Hi @cwomack , thank you for your prompt response, sir! And yes, I agree with you that this issue may be more jest-related rather than amplify-related, and also took a look at the links you shared with me. Well, I did the same thing as other guys, I mean the transformIgnorePatters thing. The moment I saw the error SyntaxError: Unexpected token 'export', I changed the configuration for transformIgnorePatters as I mentioned above in the bug description section. And then it led me to the issue that we talk about here, haha.

Anyways, instead of keeping getting stuck with this issue, I wrote a script using aws cli to test out my resources in the real amplify backend environment, even though it is a bit time-consuming. That said, I will keep looking at this issue, and update if I found something.

And again, thank you for the help, sir!

MyNameIsTakenOMG avatar Jul 04 '24 14:07 MyNameIsTakenOMG

@MyNameIsTakenOMG, we'll transfer this over to the amplify-backend repo after reviewing it further internally and update this to a feature request to expose the namespace key.

cwomack avatar Sep 25 '24 17:09 cwomack

I'm also running into this while testing with Vitest. Hopefully there'll be some guidance on this.

naedx avatar May 05 '25 12:05 naedx