NotionX / react-notion-x

Fast and accurate React renderer for Notion. TS batteries included. ⚡️
https://react-notion-x-demo.transitivebullsh.it
MIT License
4.87k stars 570 forks source link

notion-client Import Error in jest #303

Closed KIMSEUNGGYU closed 2 years ago

KIMSEUNGGYU commented 2 years ago

Description

I use @notionhq/client and notion/client and test

When testing @notionhq/client, it is performed Normally However, when testing the notion/client, the following error occurs.

error messag

image

 FAIL  src/apis/index.test.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /home/gyu/git/blog/node_modules/notion-client/build/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){var j=Object.defineProperty,Z=Object.defineProperties;var z=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var Q=Object.prototype.hasOwnProperty,Y=Object.prototype.propertyIsEnumerable;var J=(d,t,s)=>t in d?j(d,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):d[t]=s,n=(d,t)=>{for(var s in t||(t={}))Q.call(t,s)&&J(d,s,t[s]);if(x)for(var s of x(t))Y.call(t,s)&&J(d,s,t[s]);return d},m=(d,t)=>Z(d,z(t));import G from"got";import H from"p-map";import{parsePageId as M,getPageContentBlockIds as T,uuidToId as K,getBlockCollectionId as W}from"notion-utils";var X=class{constructor({apiBaseUrl:t="https://www.notion.so/api/v3",authToken:s,activeUser:e,userTimeZone:a="America/New_York"}={}){this._apiBaseUrl=t,this._authToken=s,this._activeUser=e,this._userTimeZone=a}async getPage(t,{concurrency:s=3,fetchMissingBlocks:e=!0,fetchCollections:a=!0,signFileUrls:i=!0,chunkLimit:o=100,chunkNumber:h=0,gotOptions:l}={}){var p,B,R;let y=await this.getPageRaw(t,{chunkLimit:o,chunkNumber:h,gotOptions:l}),r=y==null?void 0:y.recordMap;if(!(r!=null&&r.block))throw new Error(`Notion page not found "${K(t)}"`);if(r.collection=(p=r.collection)!=null?p:{},r.collection_view=(B=r.collection_view)!=null?B:{},r.notion_user=(R=r.notion_user)!=null?R:{},r.collection_query={},r.signed_urls={},e)for(;;){let k=T(r).filter(c=>!r.block[c]);if(!k.length)break;let b=await this.getBlocks(k,l).then(c=>c.recordMap.block);r.block=n(n({},r.block),b)}let _=T(r);if(a){let k=_.flatMap(b=>{var v;let c=r.block[b].value,f=c&&(c.type==="collection_view"||c.type==="collection_view_page")&&W(c,r);return f?(v=c.view_ids)==null?void 0:v.map(O=>({collectionId:f,collectionViewId:O})):[]});await H(k,async b=>{var O,U;let{collectionId:c,collectionViewId:f}=b,v=(O=r.collection_view[f])==null?void 0:O.value;try{let g=await this.getCollectionData(c,f,v,{gotOptions:l});r.block=n(n({},r.block),g.recordMap.block),r.collection=n(n({},r.collection),g.recordMap.collection),r.collection_view=n(n({},r.collection_view),g.recordMap.collection_view),r.notion_user=n(n({},r.notion_user),g.recordMap.notion_user),r.collection_query[c]=m(n({},r.collection_query[c]),{[f]:(U=g.result)==null?void 0:U.reducerResults})}catch(g){console.warn("NotionAPI collectionQuery error",t,g.message),console.error(g)}},{concurrency:s})}return i&&await this.addSignedUrls({recordMap:r,contentBlockIds:_,gotOptions:l}),r}async addSignedUrls({recordMap:t,contentBlockIds:s,gotOptions:e={}}){t.signed_urls={},s||(s=T(t));let a=s.flatMap(i=>{var h,l,y,r,_;let o=t.block[i].value;if(o&&(o.type==="pdf"||o.type==="audio"||o.type==="image"&&((h=o.file_ids)==null?void 0:h.length)||o.type==="video"||o.type==="file"||o.type==="page")){let p=o.type==="page"?(l=o.format)==null?void 0:l.page_cover:(_=(r=(y=o.properties)==null?void 0:y.source)==null?void 0:r[0])==null?void 0:_[0];if(p)return p.indexOf("youtube")>=0||p.indexOf("vimeo")>=0?[]:{permissionRecord:{table:"block",id:o.id},url:p}}return[]});if(a.length>0)try{let{signedUrls:i}=await this.getSignedFileUrls(a,e);if(i.length===a.length)for(let o=0;o<a.length;++o){let h=a[o],l=i[o];t.signed_urls[h.permissionRecord.id]=l}}catch(i){console.warn("NotionAPI getSignedfileUrls error",i)}}async getPageRaw(t,{gotOptions:s,chunkLimit:e=100,chunkNumber:a=0}={}){let i=M(t);if(!i)throw new Error(`invalid notion pageId "${t}"`);let o={pageId:i,limit:e,chunkNumber:a,cursor:{stack:[]},verticalColumns:!1};return this.fetch({endpoint:"loadPageChunk",body:o,gotOptions:s})}async getCollectionData(t,s,e,{limit:a=9999,searchQuery:i="",userTimeZone:o=this._userTimeZone,loadContentCover:h=!0,gotOptions:l}={}){var B,R,k,b,c,f,v,O;let y=e==null?void 0:e.type,r=y==="board",_=((B=e==null?void 0:e.format)==null?void 0:B.board_columns_by)||((R=e==null?void 0:e.format)==null?void 0:R.collection_group_by),p=m(n({type:"reducer",reducers:{collection_group_results:{type:"results",limit:a,loadContentCover:h}},sort:[]},e==null?void 0:e.query2),{searchQuery:i,userTimeZone:o});if(_){let U=((k=e==null?void 0:e.format)==null?void 0:k.board_columns)||((b=e==null?void 0:e.format)==null?void 0:b.collection_groups)||[],g=[r?"board":"group_aggregation","results"],$={checkbox:"checkbox_is",url:"string_starts_with",text:"string_starts_with",select:"enum_is",multi_select:"enum_contains",created_time:"date_is_within",undefined:"is_empty"},q={};for(let S of U){let{property:L,value:{value:u,type:P}}=S;for(let N of g){let A=N==="results"?{type:N,limit:a}:{type:"aggregation",aggregation:{aggregator:"count"}},I=typeof u>"u",C=u==null?void 0:u.range,D=I?"uncategorized":C?((c=u.range)==null?void 0:c.start_date)||((f=u.range)==null?void 0:f.end_date):(u==null?void 0:u.value)||u,F=!I&&(C||(u==null?void 0:u.value)||u);q[`${N}:${P}:${D}`]=m(n({},A),{filter:{operator:"and",filters:[{property:L,filter:n({operator:I?"is_empty":$[P]},!I&&{value:{type:"exact",value:F}})}]}})}}let E=r?"board_columns":`${y}_groups`;p=m(n({type:"reducer",reducers:n({[E]:m(n({type:"groups",groupBy:_},((v=e==null?void 0:e.query2)==null?void 0:v.filter)&&{filter:(O=e==null?void 0:e.query2)==null?void 0:O.filter}),{groupSortPreference:U.map(S=>S==null?void 0:S.value),limit:a})},q)},e==null?void 0:e.query2),{searchQuery:i,userTimeZone:o})}return this.fetch({endpoint:"queryCollection",body:{collection:{id:t},collectionView:{id:s},loader:p},gotOptions:l})}async getUsers(t,s){return this.fetch({endpoint:"getRecordValues",body:{requests:t.map(e=>({id:e,table:"notion_user"}))},gotOptions:s})}async getBlocks(t,s){return this.fetch({endpoint:"syncRecordValues",body:{requests:t.map(e=>({table:"block",id:e,version:-1}))},gotOptions:s})}async getSignedFileUrls(t,s){return this.fetch({endpoint:"getSignedFileUrls",body:{urls:t},gotOptions:s})}async search(t,s){let e={type:"BlocksInAncestor",source:"quick_find_public",ancestorId:M(t.ancestorId),sort:"Relevance",limit:t.limit||20,query:t.query,filters:n({isDeletedOnly:!1,isNavigableOnly:!1,excludeTemplates:!0,requireEditPermissions:!1,ancestors:[],createdBy:[],editedBy:[],lastEditedTime:{},createdTime:{}},t.filters)};return this.fetch({endpoint:"search",body:e,gotOptions:s})}async fetch({endpoint:t,body:s,gotOptions:e,headers:a}){let i=m(n(n({},a),e==null?void 0:e.headers),{"Content-Type":"application/json"});this._authToken&&(i.cookie=`token_v2=${this._authToken}`),this._activeUser&&(i["x-notion-active-user-header"]=this._activeUser);let o=`${this._apiBaseUrl}/${t}`;return G.post(o,m(n({},e),{json:s,headers:i})).json()}};export{X as NotionAPI};

                                   ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      3 | import { Post, MultiSelectType } from '@/types/index';
      4 |
    > 5 | import { NotionAPI } from 'notion-client';
        |                                          ^
      6 | const api = new NotionAPI();
      7 |
      8 | export async function getDetailPost(postId: string) {

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (src/apis/index.ts:5:42)

config

const createJestConfig = nextJest({ // Provide the path to your Next.js app to load next.config.js and .env files in your test environment dir: './', });

// Add any custom config to be passed to Jest const customJestConfig = { setupFilesAfterEnv: [ 'given2/setup', // '/jest.setup.js', ], moduleNameMapper: { // Handle module aliases (this will be automatically configured for you soon) '^@/components/(.)$': '/src/components/$1', '^@/containers/(.)$': '/src/containers/$1', '^@/pages/(.)$': '/pages/$1', '^@/(.)$': '/$1', }, testEnvironment: 'jest-environment-jsdom', collectCoverageFrom: ['src/*/.{ts,tsx}', '!src/styles/*'], // coverage all file };

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async module.exports = createJestConfig(customJestConfig);


#### Notion Test Page ID

sorry, privacy data...

<!--
Please include the ID of at least one publicly accessible Notion page related to your issue.

This is extremely helpful for us to debug and fix issues.

Thanks!
-->
transitive-bullshit commented 2 years ago

react-notion-x only supports ESM. The error is telling you to make sure that ESM is properly configured in your Jest config: https://jestjs.io/docs/ecmascript-modules

9oelM commented 2 years ago

I recently find lots of people being confused about esm and cjs, here on this repo as well actually. Perhaps a short docs on this might be a good idea?

KIMSEUNGGYU commented 2 years ago

I solved it. Apply ESM to Jest

module.exports = createJestConfig(customJestConfig);


- **after**

// jest.config.js

module.exports = async () => ({ ...(await createJestConfig(customJestConfig)()), transformIgnorePatterns: ['node_modules/(?!(notion-client)/)'], });



- https://github.com/KIMSEUNGGYU/blog/pull/58/commits/0b653a81a3b28ee3b4e147fa83e1b4df3c563eae